Home > Web Front-end > Front-end Q&A > There are several basic data types in ecmascript

There are several basic data types in ecmascript

青灯夜游
Release: 2021-12-06 15:52:56
Original
2210 people have browsed it

There are 5 basic data types in ecmascript: 1. Undefined type, which means undefined; 2. Null type, which means an "empty" value, that is, there is no value, and is often used to define null object pointers; 3. , Number type; 4. String type; 5. Boolean type.

There are several basic data types in ecmascript

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

There are 5 basic data types in ECMAScript: Undefined, Null, Number, String, Boolean, and a complex data type Object (composed of unordered name-value pairs).

A summary of several data types that are not very clear is summarized as follows:

1. Use the typeOf operator to determine the data type of the variable

  • "undefined"——If the value is undefined;

  • "boolean"——If the value is a Boolean value;

  • "string"——If the value is a string;

  • "number"——If the value is a numerical value;

  • "object "——If the value is an object or null;

  • "function"——If the value is a function;

Example:

var message = "some string";
alert(typeOf message); //"string"
alert(typeOf(message)); //"string"
alert(typeOf  95); //"number" 
Copy after login

2. Undefined type (the value is undefined)

If the var variable is declared but not initialized, it is undefined.

Example:

var  message;
alert(message == undefined) ;    //true
Copy after login

3. Null type (value is null)

The null value represents a null object pointer.

Example:

var   obj = null;
alert(typeOf obj);  //"object"
Copy after login

4, Number type

There are many values ​​of this type, focus on NaN. NaN represents the original value The operand that returns a numeric value does not return a numeric value, that is, any numeric value divided by a non-numeric value in ECMAScript will return NaN. NaN has two characteristics: first, any operation involving NaN will return NaN; second, NaN is not equal to any value, including NaN itself.

Example:

alert(NaN == NaN);    //false
Copy after login

isNaN() function. This function receives a parameter. This parameter can be of any type. The isNaN() function can be used to determine whether the parameter is "not a numerical value". This function After receiving a value, an attempt is made to convert it to a numeric value. Any value that cannot be converted to a numeric value will cause this function to return true.

Example:

alert(isNaN(NaN)) ;    //true
alert(isNaN(10));  // false
alert(isNaN("10"));  //false
alert(isNaN("blue"));//true(不能转换成数值类型)
alert(isNaN(true)); // false (true可以转换成1)
Copy after login

5. String type

The string (String) type is a period wrapped in single quotes '' or double quotes "" text, such as '123', "abc". It should be noted that single quotes and double quotes are different ways of defining a string and are not part of the string.

When defining a string, if the string contains quotation marks, you can use backslash\ to escape the quotation marks in the string, or choose different quotation marks from the string to define the string, as shown in the following example Display:

var str = "Let's have a cup of coffee.";  // 双引号中包含单引号
var str = 'He said "Hello" and left.';    // 单引号中包含双引号
var str = 'We\'ll never give up.';        // 使用反斜杠转义字符串中的单引号
Copy after login

6. Boolean type

The Boolean type has only two values, true (true) or false (false). When making conditional judgments It is commonly used. In addition to directly using true or false to define Boolean type variables, you can also use some expressions to get Boolean type values, such as:

var a = true;   // 定义一个布尔值 true
var b = false;  // 定义一个布尔值 false
var c = 2 > 1;  // 表达式 2 > 1 成立,其结果为“真(true)”,所以 c 的值为布尔类型的 true
var d = 2 < 1;  // 表达式 2 < 1 不成立,其结果为“假(false)”,所以 c 的值为布尔类型的 false
Copy after login

[Related recommendations: JavaScript learning tutorial

The above is the detailed content of There are several basic data types in ecmascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template