What are the JS reference data types? There are six data types in JS: Undefined, Null, Boolean, Number, String and Object. The first five are simple data types, which are also basically commonly used js data types, and the last one is a complex data type. So how are these data types converted? Let’s take a look at the introduction below.
1. Declare variables
One statement, multiple variables
You can declare many variables in one statement. The statement starts with var and uses commas to separate variables:
var name="Gates", age=56, job="CEO";
The statement can also span multiple lines:
var name="Jim", age=32, job="CEO";
2. Explicit data type conversion
1. Convert numbers
There are three functions that can convert non-numeric values into numeric values: Number(), parseInt(), parseFloat(). Number can be used for any data type, while the other two functions are specifically used to convert strings into numbers.
1.Number conversion:
The conversion rules are as follows:
1) If it is a Boolean value, true and false will be converted to 1 and 0 respectively.
2) If it is a numeric value, it is simply passed in and returned.
3) If it is a null value, return 0.
4) If it is undefined, return NaN.
5) If it is a string, follow the following rules:
· If the string only contains numbers, convert it to a decimal value;
· If If the string contains a valid floating point format, such as "1.1", convert it to the corresponding floating point value.
· If the string contains a valid hexadecimal format, such as "0xf", convert it to a decimal integer of the corresponding size;
· If If the string is empty, it is converted to 0;
· Others, it is converted to NaN.
6) If it is an object, call the valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the object's toString() method, and then convert the returned string value again according to the previous rules.
var num1 = Number("hello world"); //NaN
var num2 = Number(""); //0
var num3 = Number("0011"); //11
var num4 = Number(true); //12.parseInt()
Ignore spaces in front of the string until the first non-empty character is found, and subsequent non-numeric characters will be ignored; if the first character is not a numeric symbol or a negative sign, returns NaN; the decimal will be rounded (rounded down)
var num1 = parseInt("1234blue"); //1234
var num2 = parseInt(" "); //NaN
var num3 = parseInt(22.5): //223.parseFloat()
The same as parseInt, the only difference is that parseFloat can retain decimals.
2. Convert to string
You can convert other data types into strings.
1. Transformation function String()String() function follows the following transformation rules:
1) If the value has a toString() method, call the method and return the corresponding result;
2) If the value is null, return "null";
3) If the value is undefined, return "undefined";
2. ToString() method to convert
var num1 = 10; var str1 = num1.toString(); //"10"
Note:
When calling the toString() method of a value, you can pass a parameter: the parameter of the output value. Through this value, you can output a value expressed in binary, octal, hexadecimal, or even any other valid base format. String value.
var num = 10; num.toString(); //"10" num.toString(2); //"1010" num.toString(8); //"12" num.toString(10); //"10" num.toString(16); //"a"
3. Convert to boolean type
The literal values true and false of Boolean type are case-sensitive. Any non-empty string, any non-zero numeric value, and any object can be converted to true, while empty strings, 0 and NaN, null, and undefined are false.
var mes = "hell0"; var mesBool = Boolean(mes); //true
3. Implicit conversion
1. Convert to number
var num = "123"; num = +num;
Addition, subtraction, multiplication, division and the remainder can make the string implicitly converted to number.
2. Convert to string
var str = 123; str = str + "";
3. Convert to boolean
var bool = 123; bool = !!bool;
Related recommendations:
js data types and JS basic data What are the specific types
Summary of JS data type conversion
The above is the detailed content of What are the js data types? How to convert between these js data types?. For more information, please follow other related articles on the PHP Chinese website!
Python vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AMPython is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AMI built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AMThis article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base
JavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AMJavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






