search
HomeWeb Front-endJS TutorialDetailed explanation of the usage of six major data types in javascript basic tutorial

There are six data types in js, including five basic data types (Number, String, Boolean, Null, Undefined), and one mixed data type (Object).

Variables in js are loosely typed, so sometimes we need to detect the data type of the variable.

1.Number type

The Number type contains two values: integers and floating point numbers (floating point numbers must include a decimal point and at least one digit after the decimal point).

Floating point numbers will be automatically converted to integers.


var num = 1.00;
console.log(num);//1,自动转换为整数

The highest precision of floating point numbers is 17 digits. Looking at the example below, the result is not 0.3. As for other programming languages, this will also happen (about floating point numbers). Point calculation will cause rounding errors, and similar situations will occur in some other programming languages)


##

var num1 = 0.1;
var num2 = 0.2;
console.log(num1 + num2);//0.30000000000000004

NaN: Non-numeric type. Features: ① Any operation involving NaN will return NaN ② NaN is not equal to itself.


var ab = "a1";
console.log(ab/10);// NaN
console.log(NaN == NaN);// false;

isNaN() function is used to determine whether it is a non-numeric type. If the parameter passed in is a non-numeric type, then return true; otherwise, return false;

isNaN() function, when a parameter is passed in, the function will first convert the parameter into a numerical value.

If the parameter type is an object type, the valueOf() method of the object will be called first, and then it will be determined whether the value returned by the method can be converted to a numeric type. If not, call the object's toString() method again and determine the return value.

Numeric conversion:

Number() transformation function, which can be used for any data type;

parseInt(), Convert the value to an integer type, which is often used;

parseFloat(); Convert the value to a floating point type.

2.String type

The single quotes and double quotes in the string type have exactly the same effect.

Strings have length attributes. You can get the length of the string. var str = “hello”;console.log(str.length);//5

The value of the string is immutable. To change the value of a string, first destroy the original string and then fill it with another string containing the new value.


var lang = “java”;
lang += “script”;

The above code first creates a string that can hold 10 characters, then fills the string with java and script strings, and finally destroys the original String java and scrip string, because these two strings are useless at this time. This process happens in the background.

String conversion: Transformation function String(), applicable to any data type (null, undefined will be converted to null and undefined); toString() method (null, defined does not have toString() method).


var bc = "lijinwen";
var bd = null;
var be = undefined;
console.log(bc.toString());
//console.log(bd.toString());//error 报错
//console.log(be.toString());//error 报错
console.log("------");
console.log(String(bc));
console.log(String(bd));
console.log(String(be));

3.Boolean type

This type has only two values, true and false

Convert to boolean value: Transformation function Boolean() converts a value to Boolean type. Details will be added later.

4.Null type

The null type is regarded as a null object pointer. As mentioned above, the null type is also a null object reference. There is only one value, the null value, so when you use the typeof operator to detect a value of type null, the result is of type object.

If you define a variable, but want to use the variable as an object later, it is best to initialize the object to a null value.

5.Undefined type

has only one value, the undefined value. If a variable is declared using var, but the variable is not initialized, the value of the variable is undefined.


var name = "lijinwen";
var age;
console.log(name);//lijinwen
console.log(age);//undefined
//console.log(height);//error,变量height没有声明
console.log(typeof name);//string
console.log(typeof age);//undefined
console.log(typeof height);//undefined,变量height没有声明

In the above code, although age is declared, there is no initialization value, so what is printed is undefined. The variable height is not declared, so an error is reported.

But when using the typeof operator to detect the data type, the variable age is not initialized, and the result is undefined. The type detected by height without a declared variable is also undefined.

6.Object An object in type

js is a collection of properties and methods. The specific creation method of objects and various characteristics of objects will be introduced in detail in the later chapters of reference types. Here is a brief introduction to Object.

among the six major data types ①Constructor attribute: Constructor attribute, which can determine the constructor of the current object.


var o = new Object();
console.log(o.constructor == Object);//true
var arr = new Array();
console.log(arr.constructor == Array);//true

The above is the detailed content of Detailed explanation of the usage of six major data types in javascript basic tutorial. 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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python 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 ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python 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.

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 Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

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.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor