Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparision is not working in Javascript with Strict Equal

Tags:

javascript

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));
like image 692
Mahesh G Avatar asked Feb 17 '18 14:02

Mahesh G


People also ask

Why is {} === {} false?

{} 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.

Is == and === same in JavaScript?

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.

Can I use == to compare strings in JavaScript?

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 (===)”.

How === works in JavaScript?

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.


2 Answers

!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.

like image 183
axiac Avatar answered Oct 26 '22 15:10

axiac


Types

!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.

Example

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
like image 23
JO3-W3B-D3V Avatar answered Oct 26 '22 16:10

JO3-W3B-D3V