Equality operator
Your apps will sometimes need to check if the values in their code are equivalent or not, and then possibly perform some specific action using an if, if-else, or while block. == returns true if the value on the left-hand side of the operator is equal to the value on the right-hand side of the operator.
Examples
Example: Basic numeric equality check
// Basic numeric equality check.
var x = 5;
var y = 4;
console.log(x == 5);
console.log(x == y);
Example: "Alan Turing" equals "ALAN TURING"?
Basic string equality check. Case matters for string comparison.
// Basic string equality check. Case matters for string comparison.
var x = "Alan Turing";
var y = "ALAN TURING";
console.log(x == "Alan Turing");
console.log(x == y);
Example: 5 equals "5"?
Numeric string to number conversion is automatic in App Lab.
// Numeric string to number conversion is automatic in App Lab.
var x = 5;
var y = "5";
if(x == y)
{
console.log("equivalent")
}
else
{
console.log("not equivalent")
}
Example: 5 equals "five"?
Word string to number conversion is not automatic in App Lab.
// Word string to number conversion is not automatic in App Lab.
var x = 5;
var y = "five";
if(x == y)
{
console.log("equivalent")
}
else
{
console.log("not equivalent")
}
Syntax
__ == __
Parameters
Name | Type | Required? | Description |
---|---|---|---|
___ | any | The operands can be a number/string/boolean, or a variable containing a number/string/boolean, or the number/string/boolean returned by a function, or the number/string/boolean result of the evaluation of an expression. |
Returns
Tips
- = is the assignment operator. == is the boolean check for equivalency operator.
- JavaScript will automatically perform type conversion for you when comparing two values (e.g. the integer 5 will register as equivalent to the string "5").
- Comparison operators include < <= == > >= !=
Found a bug in the documentation? Let us know at support@code.org.