I have below Basic Javascript to reverse the number which work when I use
while (!no == 0)
But Doesn't work when I use
while (!no === 0)
I have tried in the console for parseInt(0)
which returns number
only and my no
is already number
so
why
===
is not working ,Can someone help and explain me better?
function findRepeated(number) {
var a, no, b, temp = 0;
no = parseInt(number);
while (!no == 0) {
a = no % 10;
no = parseInt(no / 10);
temp = temp * 10 + a;
}
return temp;
}
console.log(findRepeated(123));
{} creates an new object. The comparison operator ==, when used with objects, checks whether two objects are the same one. Since there are two {} in the statement {} == {}, two new objects are created separately, and then they are compared. Since they are not the same object, the result is false.
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
!no == 0
is not the same as !(no == 0)
. It is (!n) == 0
.
Similar, !no === 0
is (!no) === 0
.
This always evaluates to false
because !no
is a boolean and 0
is a number.
Values of different types are never ===
.
Read about the logical NOT operator, the comparison operators and operator precedence.
!no
returns a boolean, it returns false, although the value of no is a number, adding the negate operator in front of any number will return a false, i.e. console.log(!123 === false)
. As you're probably aware, using ==
will convert the data type for you, where as using ===
will not.
I like the example of null and undefined.
While null == undefined
is true, null === undefined
is false, because the value null is a type of object, whereas the type of undefined is of course undefined.
console.log("test1: " + !123); // false
console.log("test2: " + (!123 === false)); // true
console.log("test3: " + (!123 == 0)); // true
console.log("test4: " + (!123 === 0)); // false
console.log("test5: " + (0 == false)); // true
console.log("test6: " + (0 === false)); // false
console.log("test7: " + (null == undefined)); // true
console.log("test8: " + (null === undefined)); // false
console.log("test9: " + typeof null); // object
console.log("test10: " + typeof undefined); // undefined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With