Skip to content

JavaScript if equals | Example code

JavaScript if equals means checking the equal condition in the if statement. Equals operator return true if tow operrand value are same.

if (x === 5){
   //code
}

If expression is true then if block code will execute.

// ===	means equal value and equal type
var x = 5

// true
x === 5

// false
x === "5"

JavaScript if equals

Simple example code If variable equals the number 1.

<!DOCTYPE html>
<html>
<body>
  <script>
    var current_slide = 1;
    if (current_slide == 1) {
      alert(true)
    }
  </script>
</body>
</html>

Output:

JavaScript if equals

Comparion equal operator

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory, reference.

Do comment if you have any doubts or suggestions on this Js equal operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading