Inequality in value and type in JavaScript
In addition to the != operator, there
is also the !== operator, which takes
type into account when comparing. Let's look
at the differences between them with examples.
Let the operator != compare two numbers
3. This operator compares values to the
fact that they are NOT equal. Since our values
are just the same, the condition will be false:
if (3 != 3) {
console.log('+++');
} else {
console.log('---'); // it will work
}
Now let one of our values be in quotes. In this
case, the operator != will still consider
them equal (since the value is the same, and the
type is not important for this operator) and again
the condition will be false:
if ('3' != 3) {
console.log('+++');
} else {
console.log('---'); // it will work
}
Let's now compare the two numbers 3
using the operator !==. He will also
consider them equal:
if (3 !== 3) {
console.log('+++');
} else {
console.log('---'); // it will work
}
But if we now quote one of the threes, then
the operator !== will consider them
unequal, since, although their values are the
same, they have a different type:
if ('3' !== 3) {
console.log('+++'); // it will work
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test1 = '3';
let test2 = '3';
if (test1 != test2) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test1 = '3';
let test2 = '3';
if (test1 !== test2) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test1 = 3;
let test2 = '3';
if (test1 != test2) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test1 = 3;
let test2 = '3';
if (test1 !== test2) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test1 = 3;
let test2 = 2;
if (test1 !== test2) {
console.log('+++');
} else {
console.log('---');
}