Why do you want to say this?
An interview question gives me the motivation for talking about it.
The question is as follows:
var bool = new Boolean(false); if (bool) { alert('true'); } else { alert('false'); }
The running result is true! ! !
Actually, type conversion, operator priority, these things are the most basic.
There is a detailed introduction in the rhinoceros book. But I rarely read the first 5 chapters of the Rhino book. . .
For example, regarding priorities, many books teach us, "There is no need to memorize the priority order. If you are not sure, just add parentheses."
We usually do this when writing code.
But what is the reality? This kind of question will be asked during the interview and you will be asked to answer it. . .
I really don’t know the meaning of this kind of question. . .
The complaints stop here. This article attempts to solve the type conversion problem and try to memorize the table on page 49 of the "JS Authoritative Guide".
What are the false values?
6 in total:
0或+0、-0,NaN "" false undefined null
The above order is arranged according to the basic types.
Nothing else! ! Even if it is in the following form:
Infinity '0'、'false'、" "(空格字符) 任何引用类型:[],{},function(){}
The correct way to understand if (a && b) is: a && b evaluates the expression and then converts it to the Boolean type.
&& is a short-circuit syntax. After evaluation, it is not necessarily a Boolean type, nor is it converted into Boolean values on both sides and then operated.
For example, the result of 2&&3 is 3, not true.
So if(a && b), what we usually understand, "if a and b are true at the same time", is a wrong way of describing it.
Convert other basic types to strings, which is basically the same as expected:
console.log("" + null); // "null" console.log("" + undefined); // "undefined" console.log("" + false); // "false" console.log("" + true); // "true" console.log("" + 0); // "0" console.log("" + NaN); // "NaN" console.log("" + Infinity); // "Infinity"
Convert other basic types to numbers, which requires special memory:
console.log(+null); // 0 console.log(+undefined); // NaN console.log(+false); // 0 console.log(+true); // 1 console.log(+""); // 0 console.log(+'1'); // 1 console.log(+'1x'); // NaN
Where null, the empty character is 0, and undefined is NaN.
Above, the basic type conversions are explained clearly.
Let’s take a look at converting reference types into basic types.
Convert reference type to boolean, always true
Convert reference type to string
1.优先调用toString方法(如果有),看其返回结果是否是原始类型,如果是,转化为字符串,返回。 2.否则,调用valueOf方法(如果有),看其返回结果是否是原始类型,如果是,转化为字符串,返回。 3.其他报错。
Convert reference type to number
1.优先调用valueOf方法(如果有),看其返回结果是否是基本类型,如果是,转化为数字,返回。 2.否则,调用toString方法(如果有),看其返回结果是否是基本类型,如果是,转化为数字,返回。 3.其他报错。
First let’s look at what common reference types toString and valueOf return?
var a = {}; console.dir(a.toString()); // "[object Object]" console.dir(a.valueOf()); // 对象本身 var b = [1, 2, 3]; console.dir(b.toString()); // "1,2,3" console.dir(b.valueOf()); // 对象本身 var c = [[1],[2]]; console.dir(c.toString()); // "1,2" console.dir(c.valueOf()); // 对象本身 var d = function() {return 2}; console.dir(d.toString()); // "function() {return 2}" console.dir(d.valueOf()); // 对象本身
So the corresponding conversion to strings and numbers is:
var a = {}; console.dir(a + ""); // "[object Object]" console.dir(+a); // NaN var b = [1, 2, 3]; console.dir(b + ""); // "1,2,3" console.dir(+b); // NaN var c = [[1],[2]]; console.dir(c + ""); // "1,2" console.dir(+c); // NaN var d = function() {return 2}; console.dir(d + ""); // "function () {return 2}" console.dir(+d); // NaN
Another error situation:
var a = {}; a.toString = function() {return {};} console.log("" + a); // 报错 console.log(+a) // 报错
The above type conversion rules are basically finished.
Finally, let’s talk about the evil “==”
The interview questions are as follows:
var a = false; var b = undefined; if (a == b) { alert('true'); } else { alert('false'); }
I thought true would pop up. Oh my God! Why is it false?
Haha. . .
Double equal sign, if the types of both sides are different, implicit conversion will occur. Rhino book page 75 summarizes it as follows:
1,null和undefined,相等。 2,数字和字符串,转化为数字再比较。 3,如果有true或false,转换为1或0,再比较。 4,如果有引用类型,优先调用valueOf。 5,其余都不相等。
Therefore:
console.log(undefined == false); // false console.log(null == false); // false console.log(0 == false); // true console.log(NaN == false); // false console.log("" == false); // true
0 == false The reason why it is true is based on item 3.
The reason why "" == false is true according to Article 3 becomes "" == 0, and then according to Article 2.
Another example from Article 4:
console.log([[2]] == 2)
The above result is true for the following reasons:
[[2]]'s valueOf is the object itself, not the basic type.
The result of trying to call toString is '2'.
So it becomes a comparison of '2' and the number 2. According to Article 2, equal. WTF!!
Finally, using "===" will eliminate these problems.
End of this article.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
