Inequality operator
Your apps will sometimes need to check if the values in their code are not equivalent, 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 not equal to the value on the right-hand side of the operator.
Examples
Example: Inequality check
// Basic numeric inequality check.
var x = 5;
var y = 4;
console.log(x != 5);
console.log(x != y);
Example: "Alan Turing" inequals "ALAN TURING"?
// Basic string inequality 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.
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.
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
- 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.