search
HomeWeb Front-endJS TutorialWhat are the data types in JavaScript?

What are the data types in JavaScript?

Mar 30, 2021 pm 04:12 PM
javascripttype of data

Javascript has 9 data types, namely: String, Number, Boolean, Null, Undefined, Symbol, Object, Array, Function ( Function).

What are the data types in JavaScript?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

JavaScript data type:

Every value in the JavaScript language belongs to a certain data type. There are 9 data types in JavaScript:

  • Value type (basic type): String, Number, Boolean, Null, Unknown Definition (Undefined), Symbol.

  • Reference data types: Object, Array, Function.

Note: Symbol is a new primitive data type introduced in ES6 to represent unique values.

1. Undefined: The Undefined type has only one value, which is the special value undefined. When a variable is declared using var but is not initialized, the variable value is undefined.

2. Null: The Null type is the second data type with only one value. Its special value is Null. From a logical point of view, null is an empty object pointer. And this is why using the typeof operator to detect null values ​​will return "object".

3. Boolean: The Boolean type has two values: true

false. It should be noted that the literal values ​​true and false of Boolean type are case-sensitive. In other words, True and False (and other mixed-size forms) are not Boolean values, just identifiers.

4. Number: There are two forms of representation of this type, the first is an integer, and the second is a floating point number. Integer: can be represented by decimal, octal, and hexadecimal literal values. Floating point number: The value must contain a decimal point and there must be one digit after the decimal point.

5. String: The String type is used to represent a character sequence consisting of zero or more 16-bit Unicode characters, that is, a string. As for whether to use single quotes or double quotes, there is still no difference in js. Remember to come in pairs.

6. Symbol type

Symbols (Symbols) are newly defined by ECMAScript version 6. The symbol type is unique and cannot be modified

var s = Symbol()

The new command cannot be used before the Symbol function, otherwise an error will be reported. This is because the generated Symbol is a primitive type value, not an object

The Symbol function can accept a string as a parameter, indicating a description of the Symbol instance

7. Object: Object data type , called an object, is a collection of data and functionality (functions). It can be created using the new operator followed by the name of the object type to be created. Can also be created using literal notation. Add a property with a different name (any string including the empty string).

8. Array

JavaScript arrays are written in square brackets. The items of the array are separated by commas.

The following code declares (creates) an array named cars, containing three items (car brands):

var cars = ["Porsche", "Volvo", "BMW"];

The array index is based on zero, which means that the first item is [ 0], the second item is [1], and so on.

Arrays in ECMAScript are quite different from arrays in other languages:

  • Each item in an ECMAScript array can save any type of data;

  • The size of the ECMAScript array can be dynamically adjusted, and elements can be added or deleted from the array;

[Recommended learning: js basic tutorial

9. Function

Functions in ECMAScript are objects and have properties and methods like other reference types. Therefore, the function name is actually a pointer to the function object.

1), function declaration

function sum(num1,num2){    
    return num1+num2;
}//函数声明

var sum = function(num1,num2){  
    return num1+num2;
};     //函数表达式 这里的分号很重要

2), no overloading

function addSomeNumber(num){    
    return num + 100;
}

function addSomeNumber(num){    
    return num + 200;
}

var result = addSomeNumber(100);  //300

When creating the second function, the variable addSomeNumber that references the first function is overwritten.

3), function declaration and function expression

alert (sum(10,10));
function sum(num1,num2){    
    return num1+num2;
}

Such code can be executed normally. Before the code starts executing, the parser will first read the function declaration and add it to the execution environment. Before the code is evaluated, the JS engine will declare the functions in the first pass and put them at the top of the source code tree. But changing it to a function expression will cause an error.

4), function as value

  • Pass one function to another function like passing parameters

function callSomeFunction(someFunction,someArgument){   
    return someFunction(someArgument);
}

function add10(num){
    return num+10;
}

var result1 = callSomeFunction(add10,10);
alert(result1);  //20

function getGreeting(name){
    return "Hello"+name;
}
var result2 = callSomeFunction(getGreeting,"Mike");
alert(result2);  //Hello Mike

//callSomeFunction是通用的,函数作为第一个参数传递进去,返回执行第一个参数后的结果
  • Returning one function from another function

function createComparisonFunction(propertyName){
    return function(object1,object2){
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if(value1<value2){
            return -1;
        }else if(value1>value2){
            return 1;
        }else{
            return 0;
        }
    }
}

var date = [{name:"Mike", age:28},{name:"Amy", age:29}];//创建包含两个对象的数组
date.sort(creatComparisonFunction("name"));
alert(date[0].name);//Amy
date.sort(creatComparisonFunction("age"));
alert(date[0].name);//Mike

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the data types in JavaScript?. 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
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.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

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.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.