Table of Contents
In Javascript, there are two types of equality signs: double equal sign (==) and triple equal sign (===). The double equal sign (==) indicates value equality, while the triple equal sign (===) indicates strict equality (whether the values ​​and types are completely equal). " > In Javascript, there are two types of equality signs: double equal sign (==) and triple equal sign (===). The double equal sign (==) indicates value equality, while the triple equal sign (===) indicates strict equality (whether the values ​​and types are completely equal).
Home Web Front-end JS Tutorial Example of implicit conversion mechanism of double equal sign in Javascript

Example of implicit conversion mechanism of double equal sign in Javascript

Oct 27, 2017 am 09:28 AM
javascript js Convert

In Javascript, there are two types of equality signs: double equal sign (==) and triple equal sign (===). The double equal sign (==) indicates value equality, while the triple equal sign (===) indicates strict equality (whether the values ​​and types are completely equal).

So there are some common sense knowledge:

1. For basic types such as string, number, == and === are different
1) Comparison between different types, == compares "values ​​converted into the same type" to see if the "values" are equal, === if the types are different, the result is unequal
2) Comparison of the same type, directly Compare "value" and the result is the same

2. For advanced types such as Array and Object, there is no difference between == and ===
Compare "pointer address"

3. There is a difference between basic types and advanced types, == and ===
 1) For ==, convert the advanced types into basic types and perform "value" comparison
 2) Because the types are different, = The result of == is false

In other words, the double equal sign (==) will perform type conversion during operation, while the triple equal sign (== =) No.

For example:

alert('55' == 55); //true
alert('55' === 55); //false
Copy after login

Five basic data types in Javascript language (original value, also called simple data type): namely Undefined, Null, Boolean, Number and String types. Since these primitive types occupy a fixed amount of space, they can be stored in a smaller area of ​​memory - the stack. This storage makes it easy to quickly look up the value of the variable. (For details, see: http://www.w3school.com.cn/js/pro_js_value.asp)

The implicit conversion mechanism using the double equal sign (==) in Javascript to determine equality:

1, if both sides are simple types:

 1,1, both sides are simple types and the types are the same, then compare directly.

console.log(1==1); //true
console.log("1"=="1"); //true
console.log(false==false); //true
console.log(null==null); //true
console.log(undefined==undefined); //true
Copy after login

 1.2. Both sides are simple types. If the types are different, they will be converted to numerical comparison first (Boolean has only two values: true==1, false==0; null and undefined are equal; String Number is equal to numeric value, empty string ""==0;)

console.log(1==true); //true
console.log(0==false); //true
console.log(1=="1"); //true
console.log(0==""); //true
console.log(0==null); //false
console.log(0==undefined); //false
console.log(null==undefined); //true
Copy after login

2, if one side is a simple type and the other side is a reference type (advanced type), the advanced type is implicit Convert to a simple type and compare.

console.log(Object==Object); //true
console.log(Object=={}); //false
console.log(0=={}); //false
console.log(0==[]); //true
console.log(Array==Array); //true
console.log(Object==Array); //false
Copy after login

3, if both sides are reference types (advanced types), "pointer address" comparison is performed.

Key points-toString() and valueOf()

The first impression many people have when seeing these two methods is that the toString() method converts an object into a string, and the valueOf method converts it into a string. Convert an object to a numeric value.

This idea is very one-sided. Let’s take a look at the following two examples:

var obj={
    name:"熊仔其人",
    getName:function(){ return $(this).name; }
};

console.log(obj.toString()); //[object Object]
Copy after login

Define an obj object, call its toString method, the return value is [object Object], and find that does not return a string representation of its contents as we thought.

var arr=[1,2,3];
console.log(arr.valueOf()); //(3) [1, 2, 3]
Copy after login

Define an array arr, call its valueOf method, the return value is [1, 2, 3], and find that the representation of numeric type is not returned as we imagined.

In fact, the real understanding is this: calling the object's toString() method can convert the object into a string, but if you want to convert it into a string, you do not necessarily have to call the toString method.

Let’s take a look at the code below.

var obj= { };     
obj.valueOf=function(){ return 1; }
obj.toString=function(){ return 2; }
console.log(obj==1);    //truevar obj2= { };     
obj2.valueOf=function(){ return 2; }
obj2.toString=function(){ return 1; }
console.log(obj2==1);    //false                        
var obj3={ };
obj3.valueOf=function(){ return []; }
obj3.toString=function(){ return 1; }
console.log(obj3==1);    //true
Copy after login

In the above code, we defined an object obj, obj2, and defined the return value of the valueOf and toString methods. By comparing it with 1 for equality, we found that the valueOf method was called first.

Then an object obj3 is defined, and the return value of the valueOf and toString methods is defined. By comparing it with 1, it is found that it calls the toString method.

Then let's look at the following piece of code:

var obj= { };     
obj.valueOf=function(){ return 'a'; }
obj.toString=function(){ return 2; }
console.log(obj=='a');    //truevar obj2= { };     
obj2.valueOf=function(){ return 'b'; }
obj2.toString=function(){ return 'a'; }
console.log(obj2=='a');    //false
Copy after login

An object obj is defined in the above code 2. By comparing it with the string 'a', it is found that it calls the valueOf method.

Then the comparison between object obj2 and 'a' returns false, and it is found that the toString method is not called.

From this we can draw the conclusion:

When the object is converted to a simple type, the valueOf method will be called first. If it can be compared with a simple value, it will be compared directly. The toString method is no longer called at this time. If the valueOf method cannot be compared with a simple value after calling the valueOf method, the toString method will be called again to finally get the comparison result.

But one thing to note is that the Date object does not meet the above rules. The toString and valueOf methods of the Date object have been redefined, and the toString method will be called by default.

The above is the detailed content of Example of implicit conversion mechanism of double equal sign in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to convert AI files to CDR format How to convert AI files to CDR format Feb 19, 2024 pm 04:09 PM

AI files refer to vector graphics files created by Adobe Illustrator (AI for short) software, while CDR files refer to vector graphics files created by CorelDRAW software. Since these two softwares are developed by different manufacturers, their file formats are different and cannot be directly converted to each other. However, we can convert AI files to CDR files through some methods. A commonly used conversion method will be introduced below. Step 1: Export AI files to EPS format AdobeIllust

How to convert ODT to Word in Windows 11/10? How to convert ODT to Word in Windows 11/10? Feb 20, 2024 pm 12:21 PM

In this article, we will show you how to convert OpenDocumentTextDocument (ODT) files to Microsoft Word (Docx, DOC, etc.). Format. How to Convert ODT to Word in Windows 11/10 Here is how you can convert ODT documents to DOC or DOCX format on Windows PC: Convert ODT to Word using WordPad or Word The first method we are going to show you Is to use WordPad or MicrosoftWord to convert ODT to Word. Here are the steps to achieve this: First, open the WordPad app using the Start menu. Now, go to

How to convert a virtual machine to a physical machine? How to convert a virtual machine to a physical machine? Feb 19, 2024 am 11:40 AM

Converting a virtual machine (VM) to a physical machine is the process of migrating a virtual instance and associated application software to a physical hardware platform. This conversion helps optimize operating system performance and hardware resource utilization. This article aims to provide an in-depth look at how to make this conversion. How to implement migration from virtual machine to physical machine? Typically, the conversion process between a virtual machine and a physical machine is performed outside the virtual machine by third-party software. This process consists of multiple stages involving the configuration of virtual machines and the transfer of resources. Prepare the physical machine: The first step is to ensure that the physical machine meets the hardware requirements for Windows. We need to back up the data on a physical machine as the conversion process will overwrite the existing data. *Username and password for an administrator account with administrator rights to create system images. will be virtual

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

See all articles