Home  >  Article  >  Web Front-end  >  Detailed explanation of implicit type conversion in JS

Detailed explanation of implicit type conversion in JS

php中世界最好的语言
php中世界最好的语言Original
2018-03-17 09:28:421427browse

This time I will bring you the implicit type conversion of JSDetailed explanation, what are the notes of implicit type conversion of JS, the following is a practical case, let’s take a look .

The implicit type conversion mentioned here is the conversion caused by ==.

  1. If NaN exists, always return false

  2. Check again to see if there is a Boolean, if there is a Boolean, convert the Boolean into a number

  3. Then check to see if there is a string. There are three situations. The other party is an object, and the object is converted using toString; the other party is a number, and the string is converted to a number; If it is a string, compare it directly; if it is a number, it will return false

  4. If it is a number and the other party is an object, the object will be compared with valueOf, and if it is anything else, it will return false

  5. null, undefined will not perform type conversion, but they are equal

This sequence must be memorized, which is often asked during interviews.

Here are some miscellaneous questions, do it yourself

0 == undefined
1 == true
2 == {valueOf: function(){return 2}}
NaN == NaN
 8 == undefined
1 == undefined
 null == {toString: function(){return 2}}
 0 == null
 null == 1
 { toString:function(){ return 1 } , valueOf:function(){ return [] }} == 1

I believe you have mastered the method after reading the case in this article, please pay attention for more exciting things Other related articles on php Chinese website!

Recommended reading:

Simple and efficient JSON

##How to use the $http service in angular

Several ways of AngularJS dependency injection

Detailed explanation of the use of angular routing ui-router

The above is the detailed content of Detailed explanation of implicit type conversion in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn