Ads and ad measurement. Disabled until you allow it.
ABOUT SITE
All open site tabs
JavaScript · Syntax · Beginner
Conditions 2
11 tasks
Additional practice with conditionals in JavaScript: string checks, text length, character checks, slicing via string methods, logical operators &&/||, and simple input scenarios.
Quick topic start and explanations before exercises (exercises below):
String exercises at this level require knowing which string operations to use for comparisons and checks. JavaScript strings have a clean set of methods for this.
Character access uses bracket notation. JavaScript does not have negative indices, but the at() method does:
```javascript
const text = "JavaScript";
console.log(text[0]); // J — first character
console.log(text.at(-1)); // t — last character
console.log(text.at(-2)); // p — second to last
```
Slicing uses slice(start, end). Unlike Python, there is no step argument, but the syntax is otherwise identical:
```javascript
const text = "Hello world";
console.log(text.slice(0, 4)); // Hell — first 4 characters
console.log(text.slice(2, 8)); // llo wo
```
If you omit the second argument, slice goes to the end of the string.
For substring and prefix/suffix checks:
```javascript
text.includes("@") // true when "@" appears anywhere
text.startsWith("Py") // true when text begins with "Py"
text.endsWith("on") // true when text ends with "on"
```
All three are case-sensitive. "python".startsWith("Py") is false.
String length is a property, not a method:
```javascript
text.length // number of characters — no parentheses
```
A common mistake is writing text.length() — that will throw a TypeError.
Before accessing characters at a specific index, check that the string is long enough:
```javascript
const text = prompt("Enter a string: ");
if (text.length > 10) {
console.log(text[0], text.at(-1));
} else {
console.log("Too short");
}
```
For case-insensitive comparisons, convert to lowercase first. toLowerCase() returns a new string:
```javascript
const text = prompt("Enter a phrase: ");
if (text.toLowerCase() === "hello") {
console.log("Hello to you too!");
} else if (text.toLowerCase() === "goodbye") {
console.log("Goodbye!");
} else {
console.log("I don't understand");
}
```
Combining a length check with a content check — && short-circuits from left to right, so if the length check fails JavaScript skips the rest:
```javascript
const text = prompt("Enter a message: ");
if (text.length > 6 && text.at(-1) === "!") {
console.log("Emotional message");
} else {
console.log("Normal message");
}
```
Checking a range of lengths:
```javascript
if (text.length >= 5 && text.length <= 10) {
console.log("Suitable length");
}
```
JavaScript does not have Python's chained comparison — you always need the two separate conditions joined with &&.
The user enters a string.
If the string length is greater than 10 characters — display the first and last character,
otherwise — display "The string is too short".
let text = "JavaScript course";
Solution
let text = "JavaScript course";
if (text.length > 10) {
console.log(text[0], text[text.length - 1]);
} else {
console.log("The string is too short");
}
// or you can do it this way: store the characters in variables
let text = "JavaScript course";
if (text.length > 10) {
let first = text[0];
let last = text[text.length - 1];
console.log(first, last);
} else {
console.log("The string is too short");
}
The user enters a string.
If the string starts with the letter "A" or "a" — display "Starts with A",
otherwise — display "Starts with another letter". You may use string methods.
let text = "Alice";
Solution
let text = "Alice";
if ((text.startsWith("A")) || (text.startsWith("a"))) {
console.log("Starts with A");
} else {
console.log("Starts with another letter");
}
The user enters a string.
If the string length is greater than 8 characters — display the first 4 characters,
otherwise — display the whole string.
let text = "JavaScript";
Solution
let text = "JavaScript";
if (text.length > 8) {
console.log(text.slice(0, 4));
} else {
console.log(text);
}
// or you can do it this way: prepare the fragment first
let text = "JavaScript";
let part;
if (text.length > 8) {
part = text.slice(0, 4);
} else {
part = text;
}
console.log(part);
The user enters a string.
If the string length is greater than 6 characters and the last character is "!" —
display "Emotional message", otherwise — "Regular message".
let text = "JavaScript!";
Solution
let text = "JavaScript!";
if ((text.length > 6) && (text[text.length - 1] === "!")) {
console.log("Emotional message");
} else {
console.log("Regular message");
}
// or you can do it this way with the endsWith method
let text = "Hello!";
if (text.length > 6 && text.endsWith("!")) {
console.log("Emotional message");
} else {
console.log("Regular message");
}
The user enters a string.
If the string contains the symbol "@" or the symbol "#" —
display "Special character found", otherwise — "No special characters".
let text = "[email protected]";
if ((text.includes("@")) || (text.includes("#"))) {
console.log("Special character found");
} else {
console.log("No special characters");
}
// or you can do it this way: check characters one by one
let text = "hello@site";
if (text.includes("@")) {
console.log("Special character found");
} else if (text.includes("#")) {
console.log("Special character found");
} else {
console.log("No special characters");
}
The user enters a first name and last name.
If both the first name and the last name are not empty — display a greeting in the format: "Hello, First Name Last Name!" (use a template literal),
otherwise — "Enter valid data".
let name = "Alex";
let surname = "Smith";
Solution
let name = "Alex";
let surname = "Smith";
if ((name) && (surname)) {
console.log(`Hello, ${name} ${surname}!`);
} else {
console.log("Enter valid data");
}
// or without a template literal, using string concatenation
let name = "Alex";
let surname = "Smith";
if (name && surname) {
console.log("Hello, " + name + " " + surname + "!");
} else {
console.log("Enter valid data");
}
The user enters a string.
If the string length is greater than 10 characters — display a string fragment using slice
from the 3rd character (inclusive) to the 8th character (inclusive),
otherwise — display "Not enough characters".
let text = "JavaScript course";
Solution
let text = "JavaScript course";
if (text.length > 10) {
console.log(text.slice(2, 8));
} else {
console.log("Not enough characters");
}
The user enters a string.
If the string is not empty — display the message: "First character: X, last character: Y" (use a template literal),
otherwise — "Empty string".
let text = "JavaScript";
Solution
let text = "JavaScript";
if (text) {
console.log(`First character: ${text[0]}, last character: ${text[text.length - 1]}`);
} else {
console.log("Empty string");
}
// or you can do this: save the first and last character beforehand
let text = "JavaScript";
if (text) {
let first = text[0];
let last = text[text.length - 1];
console.log(`First character: ${first}, last character: ${last}`);
} else {
console.log("Empty string");
}
The user enters a string.
If the string length is from 5 to 10 characters inclusive —
display "Suitable length",
otherwise — "Unsuitable length".
let text = "JavaScript";
Solution
let text = "JavaScript";
if (5 <= text.length && text.length <= 10) {
console.log("Suitable length");
} else {
console.log("Unsuitable length");
}
// second option: the same thing with a separate length variable
let text = "course";
let length = text.length;
if (length >= 5 && length <= 10) {
console.log("Suitable length");
} else {
console.log("Unsuitable length");
}
The user enters a string.
If the string starts with "Java" or ends with "Script" —
display "Looks like JavaScript",
otherwise — "Does not look like JavaScript".
let text = "JavaScript";
Solution
let text = "JavaScript";
if ((text.startsWith("Java")) || (text.endsWith("Script"))) {
console.log("Looks like JavaScript");
} else {
console.log("Does not look like JavaScript");
}
"Hello - Goodbye program" If the user enters: Hello, hello, hElLo or HELLO - reply with "Hello to you too!" If the user enters: Bye, bYe, etc. - reply with "Adios!".
let text = "Привет";
Solution
let text = "Hello";
if (text.toLowerCase() === "hello") {
console.log("Hello to you too!");
} else if (text.toLowerCase() === "bye") {
console.log("Adios!");
} else {
console.log("Nah, I don't talk to strangers! Adios!");
}
No split tab
Cookie preferences
We use necessary cookies to run the site. With your permission, we can also save your site preferences and use analytics and advertising cookies to understand usage and support the project.
Open tools in tabs.Exercises, IDE tools, and trainers stay available as site tabs.
Switch without losing context.Move between explanations, code, and utilities while keeping your place.
Use the sidebar as your map.The left panels hold navigation, settings, files, libraries, and tool controls.
PythonJavaScriptSQLite
One IDE, three practical modes
Python in the browser.Run small scripts, try libraries, and practice API requests without installing anything.
JavaScript for quick experiments.Test browser-friendly code and compare ideas next to your learning materials.
SQLite for data practice.Open the database explorer to inspect tables, write queries, and learn SQL workflows locally.
TopicIDE
Work side by side with split tabs
Keep instructions visible.Open an exercise or reference page beside the IDE instead of jumping back and forth.
Compare tools while you learn.Place regex checks, explanations, and code experiments next to each other when the task needs it.
Close the split when you are done.The workspace returns to a single focused tab, and your open site tabs remain available.
Code Typing Trainer
Or plain text
This trainer is designed for a physical keyboard.Open this section on a laptop or desktop with a wide screen. Touch typing practice will not work correctly on a phone.
Speed: 0 chars/min
0 words/min
Best speed (60s): 0 chars/min
0 words/min
Errors: 0
Total time: 0.0 s
To practice touch typing, avoid looking at your physical keyboard.