Current location - Training Enrollment Network - Mathematics courses - What are the data types in javascript?
What are the data types in javascript?
Let's take a look at the data types in JavaScript first. JavaScript is a weakly typed language. At first, we didn't know the type of the variable, so we must judge the type of the variable by the stored specific value.

The data types of JavaScript are divided into basic data types and complex data types. Among them, the basic data types are: Number (number), String (string), Boolean (Boolean), Undefined (undefined) and Null (empty); Complex (reference) data types: Object, Array and Function. We can use the typeof operator to check the type of a variable. The syntax is typeof (variable) /typeof variable, and the return value of typeof is: number, string, Boolean, undefined, object and function. Today, let's take a look at the basic data types in JavaScript.

First, let's learn about the number types. Number type is number type, which is mainly used to store data and participate in mathematical operations. Number types include integers, decimals, positive numbers, negative numbers, various decimals and special numbers. We know the number type through the following code:

//? 1. 1? Ordinary integers and decimals

var? num 1? =? 10; ?

var? num2? =? 2.5; ? console.log(typeof(num 1),? What kind? num 2); ?

//? 1.2? Negative value?

var? num3? =? -0.2; ?

console.log(num3,? What kind? num 3); ?

//? 1.3? Octal, with? 0? At the beginning and not exceeding? 8? The value of, otherwise? 8? Binary?

var? num4? =? 070; ?

console.log(num4,? What kind? num 4); ? //? 56? Number?

//? 1.4? Hexadecimal, 0x? At the beginning, right? 16? Binary?

var? num5? =? 0x 10; ?

Console.log(num5, what type? num 5); ? // 15? Number?

In addition to the above number types, there are some special number types in JavaScript:

1, NaN: it is not a number. If the arithmetic operation fails to get a normal number, NaN will be returned, and NaN is not equal to NaN.

2. Infinity: infinity.

3. Power of e: 10 1e9 represents the power of 1 multiplied by 10 9.

4. There will be errors in decimal addition.

// 1. 1? South? Isn't it? Answer? Number? Arithmetic operation can't get normal numbers, will it all be returned? South? var? num5? =? One? -? 5; ?

Console.log(num5, what type? num 5); ? //? South? Number?

//? 1.2? Infinite?

var? num6? =? 1? /? 0; ?

Console.log(num6, what type? num 6); ? //? Infinite? Number?

//? 1.3? Add a decimal? There will be a little deviation.

console . log(0. 1+0.2); ? //? 0.30000000000000004?

Next, let's learn about string types. The string type is a string type, which is the content enclosed in quotation marks. Both single and double quotes are acceptable, such as "web" and "web". Strings have a common property length, which is used to represent the length of strings. Any character in a string accounts for a length of the string, and the string can also get the corresponding subscript character by charAt (subscript) method. It should be noted that the subscript in the string starts from 0, and the corresponding subscript characters can also be obtained by adding parentheses in the string in addition to the charAt () method.

var? str 1? =? ABC '; ?

var? str2? =? " 123"; ?

Console.log (type? Str 1, type of? str 2); ? //String? String?

console . log(str 1 . charat(0)); ? //? Answer?

console . log(str 1[0]]); ? //? Answer?

In addition, all the user input we get from the input form is a string, even a number. For example, the following example.

& lt input? type="text "? id = " oIn " & gt?

& lt button? Id = "oBut"> output content

oBut.onclick? =? Function? ()? {?

var? Value? =? oIn.value

Console.log (type? Value); ? //? String?

}?

Next, let's learn about Boolean types. Boolean: Boolean type, only true and false, that is, true/false.

var? bool 1? =? True; ?

var? bool2? =? Fake; ?

Console.log (type? bool 1,? What kind? bool 2); ? //? Bull? Bull?

console.log( 10? & gt? 20); ? //False?

If (10? & gt? 20){? //? What if? In parentheses, it is automatically converted to? Bull? Type?

Alert (true)?

}; ?

In addition to comparing Boolean values, there is also a formula to judge whether a condition is true: non-zero is true and non-empty is true. ".That is, 0 is false, other numbers are true, empty strings are false, and others are true.

Finally, let's meet null and undefined. Null means that the value is empty, undefined means that the variable is not initialized (the statement is not assigned) and the value is empty. Null represents an empty object, and the "object" obtained by using the typeof operator. Undefined: Undefined type. When a variable is declared unassigned, its value is undefined and its type is undefined.

var? Answer? =? null?

console . log(a); ? //? null?

Console.log (type? a); ? //? Object?

//? var? b? =? Undefined; ?

var? b; ?

console . log(b); ? //? Undefined?

Console.log (type? b); ? //? Undefined?