Home >Web Front-end >JS Tutorial >What are the data types in JavaScript?

What are the data types in JavaScript?

青灯夜游
青灯夜游Original
2021-03-30 16:12:0534225browse

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