Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comparison Operations | Basic Operations
Introduction to JavaScript
course content

Contenido del Curso

Introduction to JavaScript

Introduction to JavaScript

1. Basic Concepts
2. Variables and Data Types
3. Basic Operations
4. Conditional Statements
5. Loops
6. Functions

bookComparison Operations

Comparisons are used to define conditions, perform checks, and make decisions in JavaScript. These operations return a boolean value (true or false), which other operators use to determine certain actions.

Comparison Operators

JavaScript offers several comparison operators for comparing values:

  • Equal to (==);
  • Strict Equal To (===);
  • Not Equal To (!=);
  • Strict Not Equal To (!==);
  • Greater Than (>);
  • Greater Than or Equal To (>=);
  • Less Than (<);
  • Less Than or Equal To (<=).

Equal and Not Equal

The Equal To operation (==) returns true if the first value equals the second value and false otherwise.

Conversely, the Not Equal To operation (!=) returns true if the values are unequal.

Example:

123456789
console.log("Equal to:"); console.log("5 == 5 is", 5 == 5); console.log("5 == 4 is", 5 == 4); console.log("5 == 4.99 is", 5 == 4.99); console.log("Not equal to:"); console.log("5 != 5 is", 5 != 5); console.log("5 != 4 is", 5 != 4); console.log("5 != 4.99 is", 5 != 4.99);
copy

Note

The = operator is for assignment, while == is for comparison. Be careful to distinguish them.

Strict comparison

Strict comparison checks the types of values and values. The Strict Equal To (===) operator returns true - if both the values and their types are the same, while !== returns true if they are different.

Example:

12345678910
console.log("5 === 5 is", 5 === 5); console.log("5 === '5' is", 5 === '5'); console.log("5 == '5' is", 5 == '5'); console.log("5 !== 5 is", 5 !== 5); console.log("5 !== '5' is", 5 !== '5'); console.log("5 != '5' is", 5 != '5'); console.log("Type of 5 is", typeof(5)); console.log("Type of '5' is", typeof('5'));
copy

Greater and Less

The Greater Than (>) operator returns true if the first value exceeds the second. Conversely, the Less Than (<) operator returns true if the first value is less than the second.

Example:

1234567
console.log("5 > 5 is", 5 > 5); console.log("5 > 4.99 is", 5 > 4.99); console.log("5 > 5.0000001 is", 5 > 5.0000001); console.log("5 < 5 is", 5 < 5); console.log("5 < 4.99 is", 5 < 4.99); console.log("5 < 5.0000001 is", 5 < 5.0000001);
copy

Greater/Less Than or Equal to

The Greater Than or Equal To (>=) and Less Than or Equal To (<=) operators combine the corresponding comparison operators with Equal To (==). They return true if either of the conditions is satisfied.

1234567
console.log("5 >= 5 is", 5 >= 5); console.log("5 >= 4.99 is", 5 >= 4.99); console.log("5 >= 5.0000001 is", 5 >= 5.0000001); console.log("5 <= 5 is", 5 <= 5); console.log("5 <= 4.99 is", 5 <= 4.99); console.log("5 <= 5.0000001 is", 5 <= 5.0000001);
copy

Note

The >= operator combines > and ==, not ===. The <= operator works similarly.

1. What will be the output?
2. What will be the output?
3. What will be the output?
4. What will be the output?
What will be the output?

What will be the output?

Selecciona la respuesta correcta

What will be the output?

What will be the output?

Selecciona la respuesta correcta

What will be the output?

What will be the output?

Selecciona la respuesta correcta

What will be the output?

What will be the output?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt