CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Web development

[JS] 2 == 1 = 0 is true

npn by npn
June 23, 2019
in Web development
Reading Time: 2 mins read
0
117
VIEWS
Share on FacebookShare on Twitter
Rate this post

The other day on Twitter,@TaneoKoyama was doing an interesting tweet, so I’m going to go into this one step deeper.

console.log(2==1==0); // => true

This is really true when you try it.
interesting.

Let’s disassemble and examine one by one.

ADVERTISEMENT
console.log(2==1); // => false

↑ At this stage, it will be false …

console.log(false==0); // => true

but this is true, it is Kimo of this code.
According to the JavaScript specification, “number 0” is false.
As a result, 2==1==0there will be situations where it becomes true.

console.log(0==1==2); // => false

The result is false.
When disassembling, it is processing ….

console.log(0==1); // => false
console.log(false==2); // => false

You can confirm that the process is being performed from the left.

console.log(2===1===0); // => false

Yes, it has been false.

Difference between equality operator (==) and strict equality operator (===)

Although it is the equivalence operator “==” which is usually used casually, there was a characteristic that can be judged by type conversion.
As a result, false (boolean type) is converted to “0”, so0=0It will be judged that.

On the other hand, the strict equality operator (===) does not perform type conversion.
So false is false and 0 is 0.
In other words, even if it judges, this is not equivalent.

In the case of a similar example

console.log(null == undefined); // => true
console.log(null === undefined); // => false
console.log(0 == ''); // => true
console.log(0 === ''); // => false
  • x==y==z, it is processed from the left
  • “==” and “===” are clearly different operators.
  • Be careful about type conversion of “==” In
  • JS, 0 (numeric value), null, undefined, ” (empty character) are treated as false

that’s all.
Thank you until the end.

Tags: equality operatorJavaScriptstrict equality operator
Previous Post

Using JavaScript to vibrate the device with a web browser

Next Post

Disable browser ‘Back’ button with Javascript

npn

npn

Next Post

Disable browser 'Back' button with Javascript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result
  • About Us
  • Documentation
Together We Learn!

© 2021 Cloudreports - Developer ideas, tips and stories...

No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner

© 2021 Cloudreports - Developer ideas, tips and stories...