JS data type

Posted by jesse26 on Thu, 02 Sep 2021 21:29:46 +0200

JavaScript basic data type:

Value type (basic type): String, number, Boolean, Null, Undefined, Symbol.

Reference data types: object, array, function.

String string

A string is a variable that stores characters.

Can be any text in quotation marks. You can use single or double quotation marks:

var carname="Volvo XC60";
var carname='Volvo XC60';

You can use quotation marks in a string as long as they do not match the quotation marks surrounding the string:

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

You can use character literals to escape characters
\n line feed \ t btab \ b backspace
\r carriage return \ slash 'single quotation mark
Double quotes
The character length can be obtained through the length attribute

var str = "I'm a string";
var s4='\n\t\b\r';
Digital Number

JavaScript has only one numeric type. There are many types of numbers. According to the precision of numbers, they can be divided into integer (int), single precision (float), double precision (double). According to the representation of numbers, they can be divided into binary, octal, decimal system and hex decimal. However, in js, all numbers are represented by Number.

Representation method


Decimal 55 consists of 0 ~ 9

Octal 070 has 0 in the first place and 0 ~ 7 in other bits

Hex 0x11, the first bit is 0x, the other bits are 09, AF

var num1 = 34;         //Write without decimal point
var num2 = 010;        //8
var num3 = 0x10;       //16
console.log(x1, x2, x3, x4);
Floating point number:

The so-called floating-point value means that the value must contain a decimal point, and there must be at least one digit after the decimal point. The maximum precision of floating-point values is 17 decimal places

Ordinary floating point number 3.1415926

Scientific counting method 3.125e7, i.e. 31250000

var f1 = 3.1415926;  //3.1415926
var f2 = 3.125e7;    //31250000
console.log(f1, f2);
Non numeric:

This value indicates that an operand that was supposed to return a value did not return data

var a = 10/ "a";	// a is NaN
Non numerical detection:

Judge whether the parameter is "not a value", and return true when the parameter para is not a value

isNaN(NaN);	// true
Value range:

Due to memory constraints, ECMAScript cannot save all the values in the world.

The minimum value that ECMAScript can represent is saved in number.min_ In value

The maximum value that can be represented is saved in Number.MAX_VALUE.

If the result of a calculation exceeds the JavaScript value range, infinity (positive infinity) or - Infinity (negative infinity) will be returned

var a = 9/0;   // Infinity
Number.MIN_VALUE    5e-324
Number.MAX_VALUE   1.7976931348623157e+308
Value range detection:

Use the isfinish() function to determine whether the parameter is between the maximum and minimum values. If yes, it returns true

var a = isFinite(9/0);	// false

Boolean (logic) can only have two values: true or false.

var x=true;
var y=false;

There is only one value of this type, namely null. Null can represent a pointer to a null object.

var a = null;

If a variable is prepared to save objects in the future, it can be initialized to null instead of others, so you can know by checking the null value

Whether the corresponding variable has saved a reference to an object.

if(car !== null ){  //The car object performs some operations}

Undefined this value indicates that the variable does not contain a value. Undefined.

var a;
console.log(a,typeof a);//undefined 'undefined'
var a = undefined;
console.log(a,typeof a);//undefined 'undefined'
undefined and null relationship

Undefined inherits null, so undefined == null results in true, but null indicates an empty object, undefined indicates undefined;

The use of null is different from that of undefined. Null can be used to represent an empty object, but it is not necessary to explicitly set the value of a variable to undefined.

//null vs undefined
console.log(undefined == null); //True / / undefined derived from null
console.log(undefined === null);//false
if(null == undefined){console.log('Equal')}
if(null === undefined){console.log('Exactly equal')}
// ==, equivalent means that when the value types on both sides are different, first convert the type to the same type, and then compare whether the values are equal. 
// ===The meaning of identity is that without type conversion, the results of different types must be different.
// "= =" means that it can be true as long as the values are equal, while "= =" requires not only equal values, but also the same type.
// Suggestion: try to use strict operator = = =. Because "= =" is not rigorous, it may bring some counterintuitive consequences.
JavaScript dynamic type

Dynamic typing means that the same variable can be used as different types

var x;               // x is undefined
var x = 5;           // Now x is a number
var x = "John";      // Now x is a string

Reference data type

In js, in addition to the above basic data types, all other types can be attributed to reference data types.

Object object

An object is an object that simulates real life. An object consists of key value pairs. All key value pairs are enclosed by braces.

var dog = {
	name: 'momo',
	age: 4

You can get the properties of an object through point syntax

dog.name; //momo
dog.age;	//age
Array array

Array is a special object that contains multiple values. Values are separated by commas, and all values are enclosed by brackets.

var classArr = ['web2104','web2105','web2106']
var studentArr = ['zhangsan','lisi','wangwu']

The corresponding data can be obtained through the array subscript

classArr[0]; // web2104

A function is a code execution unit used to implement some special functions.

function sum(a, b) {
  return a + b;
//Execution function
sum(1,2); // 3

Topics: Javascript Front-end