Cityscape
Girl

JavaScript

More Operators and Expressions

Easy
30 min

More operators and expressions!

In the previous module we covered some very basic operators and expressions. Now we'll proceed to cover a few more that you'll use a lot.

Comparison Operators

Comparison operators allow you to compare two values. They evaluate to a boolean (true or false).

Equality (==) and Strict Equality (===)

When comparing values in JavaScript, it's important to understand the difference between equality (==) and strict equality (===).

The equality operator (==) checks whether two values are the same after converting them to the same type. This automatic conversion is called type coercion. For example, 5 == "5" is true because JavaScript converts the string "5" into the number 5 before comparing them.

The strict equality operator (===), on the other hand, checks both the value and the type. It does not perform type coercion (automagically converting both values to the same datatype to make, e.g., comparison possible). So 5 === "5" is false because one is a number and the other is a string—even though they look similar.

In general, it's a good idea to use === (strict equality) to avoid unexpected behavior caused by automatic type conversions.

  • 5 == "5" is true (loose equality).
  • 5 === "5" is false (strict equality, no type coercion).

Inequality (!=) and Strict Inequality (!==)

  • 5 != "6" is true.
  • 5 !== "5" is true.

Greater Than (>) and Less Than (<)

  • 10 > 3 is true.
  • 3 < 1 is false.

Greater Than or Equal (>=) and Less Than or Equal (<=)

  • 4 >= 4 is true.
  • 2 <= 1 is false.
Drawing canvas

Logical Operators

Logical operators let you combine or modify boolean values:

AND (&&)

true && false returns false. Both conditions must be true for the result to be true.

OR (||)

false || true returns true. Only one condition needs to be true.

NOT (!)

!true returns false. This operator negates a boolean value.

Drawing canvas

Ternary Operator

The ternary operator is a concise way to write conditional expressions. It follows this format:

condition ? valueIfTrue : valueIfFalse

For example:

let score = 75;
let result = score >= 60 ? "Pass" : "Fail";
console.log(result); // prints "Pass"

This single-line expression evaluates whether score is 60 or higher. If true, result becomes "Pass", otherwise "Fail".

Exercises

Compare Two Values

  • Create a variable named isEqual using let.
  • Set it to the result of comparing 5 and "5" using strict equality (===).
  • Print the value of isEqual using console.log.

Check a Value Using Logical Operators

  • Create a variable named isAdult using let and set it to true.
  • Create another variable named hasTicket using let and set it to false.
  • Create a third variable called canEnter using let that uses the AND operator (&&) to determine if the person can enter.
  • Print canEnter to the console.

Flip a Boolean with NOT

  • Create a variable called isRaining and set it to false.
  • Create another variable called goOutside which should be the opposite of isRaining using the NOT operator (!).
  • Print goOutside to the console.

Use the Ternary Operator

  • Create a variable called score and set it to a number.
  • Create another variable called grade that uses the ternary operator to assign "Pass" if the score is 60 or higher, otherwise "Fail".
  • Print grade to the console.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.