Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Comparison Operators | Building Conditions and Repetition
Conditional Statements and Loops in JavaScript

bookComparison Operators

Comparison operators are essential tools in JavaScript that allow you to check relationships between values, such as numbers and strings, so your code can make informed decisions. Imagine you are at a movie theater. To check if you are allowed to watch a certain movie, you might compare your age with the movie's minimum age requirement. JavaScript uses several comparison operators to help you perform similar checks in your programs.

The == operator checks if two values are equal, but it does not require them to be the same type. For instance, 5 == "5" is true because JavaScript converts the string "5" to the number 5 before comparing.

The === operator checks for both value and type equality, so 5 === "5" is false since one is a number and the other is a string.

When you want to check if two things are not equal, you can use != (not equal, ignoring type) or !== (not equal, considering type).

For checking sizes or order, use >, <, >=, and <=. These operators compare numbers or strings to see which is greater or if they are at least equal. For example, if a movie requires viewers to be at least 13 years old, you can check age >= 13. If you want to know if someone is younger than the required age, use age < 13.

12345678910111213141516171819202122
const userAge = 16; const movieMinAge = 13; if (userAge >= movieMinAge) { console.log("You can watch the movie!"); } else { console.log("Sorry, you're not old enough."); } const friendAge = "16"; // Using == if (userAge == friendAge) { console.log("You and your friend have the same age (==)."); } // Using === if (userAge === friendAge) { console.log("You and your friend have the same age and type (===)."); } else { console.log("You and your friend have the same age but not the same type (===)."); }
copy

In this example, the code compares a user's age to the minimum required age for a movie. It also checks if the user's age and a friend's age are equal, both by value (==) and by value and type (===). This demonstrates how different comparison operators affect the outcome of your checks.

question mark

Which operator should you use to check if two values are not equal, considering both value and type?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between == and === in more detail?

What happens if I compare two strings with these operators?

Can you give more examples of using comparison operators in JavaScript?

Awesome!

Completion rate improved to 7.69

bookComparison Operators

Swipe to show menu

Comparison operators are essential tools in JavaScript that allow you to check relationships between values, such as numbers and strings, so your code can make informed decisions. Imagine you are at a movie theater. To check if you are allowed to watch a certain movie, you might compare your age with the movie's minimum age requirement. JavaScript uses several comparison operators to help you perform similar checks in your programs.

The == operator checks if two values are equal, but it does not require them to be the same type. For instance, 5 == "5" is true because JavaScript converts the string "5" to the number 5 before comparing.

The === operator checks for both value and type equality, so 5 === "5" is false since one is a number and the other is a string.

When you want to check if two things are not equal, you can use != (not equal, ignoring type) or !== (not equal, considering type).

For checking sizes or order, use >, <, >=, and <=. These operators compare numbers or strings to see which is greater or if they are at least equal. For example, if a movie requires viewers to be at least 13 years old, you can check age >= 13. If you want to know if someone is younger than the required age, use age < 13.

12345678910111213141516171819202122
const userAge = 16; const movieMinAge = 13; if (userAge >= movieMinAge) { console.log("You can watch the movie!"); } else { console.log("Sorry, you're not old enough."); } const friendAge = "16"; // Using == if (userAge == friendAge) { console.log("You and your friend have the same age (==)."); } // Using === if (userAge === friendAge) { console.log("You and your friend have the same age and type (===)."); } else { console.log("You and your friend have the same age but not the same type (===)."); }
copy

In this example, the code compares a user's age to the minimum required age for a movie. It also checks if the user's age and a friend's age are equal, both by value (==) and by value and type (===). This demonstrates how different comparison operators affect the outcome of your checks.

question mark

Which operator should you use to check if two values are not equal, considering both value and type?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt