search
HomeWeb Front-endJS TutorialA brief discussion on data type conversion in javascript

1. Convert other data types to number types

Number()->strict

parseFloat/parseInt ->non-strict

isNaN(value ) If the value is not of numeric type, it first calls Number to convert to numeric type and then determines whether it is a valid number

For example:

Number("12px"); ->NaN

parseInt("12px"); ->12

isNaN("12"); ->false

Rule:

1) If the Boolean type is converted to a number, true->1 false->0

2) Convert null/undefined to a number null->0 undefined->NaN 10+null= 10 10+undefined=NaN

3) To convert [] to a number, first call the toString method of the array to convert []->"", and then use the Number method to convert ""->0

2. Convert other data types to Boolean types

Boolean

!!

! First convert to Boolean type, and then invert

For example:

!!1->true

!!0->false

![]->false !![]-> true

Law:

Only 0, NaN, null, undefined, and "" are false when converted to Boolean types, and the rest of the conversions are true

Special case:

If there is only a single value in your conditional judgment, it first converts 1 to Boolean type (->true), and then judges whether it is true or false. If the true condition is established, On the contrary, the condition is not true

if (1) { }
 if ("3px" * 3) {//->条件不成立 "3px" * 3=NaN ->false
 }
 if ("3px" + 3) {//->条件成立 "3px" + 3="3px3" ->true
 }

"+" is not only a mathematical operation in JS, but also a string concatenation when it encounters a string

"-, *, /" can only be mathematical operations in JS. When encountering non-number types, they must be forced to be converted to numbers for mathematical operations

3. Compare two values ​​(== for comparison, ===No default conversion will be performed if the comparison data types are different). If the data types of the left and right sides are different, we need to follow a set of rules and perform default data type conversion

value1==value2

1) Object==Object compares the memory address stored by itself. If the memory addresses are different, they are not equal []==[]->false Each empty array is a separate memory address

2) Object == String Call the toString method to convert the object to a string []==""->true

3) Object == Boolean The object is converted to a number (toString- Number), Boolean is also converted to number (true->1 false->0)

[]==false ->true []->0 false->0 0==0 ->true

![]==false -> ![]Convert the empty array to Boolean type first and then negate it->false false==false->true

4) Object == Number Object converted to number []==0->true []==1->false

5) String == Boolean Both converted to number " 3"==true->false "1"==true->true

6) String == number String converted to number ""==0->true

7)Boolean==Number Convert Boolean to number true==1->true true==2->false

8)null==undefined ->true null===undefined - >false

null/undefined is not equal to any other value null==0->false

9)NaN==NaN ->false NaN and any value ( Including myself) are not equal

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!

For more articles on data type conversion in JavaScript, please pay attention to 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
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

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.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

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

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

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.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

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.

See all articles

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 Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools