JavaScript basic data type

Posted by timandkitty on Tue, 25 Jan 2022 03:25:10 +0100

JavaScript basic data type

There are two types of JavaScript data:
Simple values (raw values): contains strings, numbers, and Boolean values, plus two special values -- null (null) and undefined (defined as).
Complex data structures (generally referred to as objects): including narrow objects, arrays and functions.

Basic type

JavaScript defines six basic data types, as shown in the table.

data typeexplain
nullNull value, indicating non object
undefinedAn undefined value that represents an unassigned initialization value
numberNumber, value of mathematical operation
stringString representing information flow
booleanBoolean value, the value of a logical operation
objectObject that represents a dataset of composite structures

Use the {typeof} operator to detect the basic type of data.
Example 1
The following code uses the {typeof} operator to detect the types of common values respectively.

console.log(typeof 1);  //Returns the string "number"
console.log(typeof "1");  //Return string "string"
console.log(typeof true);  //Return string "boolean"
console.log(typeof {});  //Return string "object"
console.log(typeof []);  //Return string "object"
console.log(typeof function(){});  //Return string "function"
console.log(typeof null);  //Return string "object"
console.log(typeof undefined) ;  //Return string "undefined"

be careful:
Typeof  operator returns one of the six basic types in the form of string. However, through comparison, it can be found that there are two differences between the return value of typeof  and the above table. The brief description is as follows:
Classify null as an Object type, not as a value of a special type (null).
Type function(,) {} as Function. That is, the Function is regarded as an independent basic data type, rather than a special subclass of Object type.
Example 2
Since the return type of the {null} value is} Object, you can avoid using the following custom function because the} null} value affects the basic type detection.

//If it is a null value, return the string "null" first, otherwise return the value of (typeof o)
function typeOf(o){
    return (o === null) ? "null" : (typeof o);
}
console.log(typeOf(1));  //Returns the string "number"
console.log(typeOf("1"));  //Return string "string"
console.log(typeOf(true));  //Return string "boolean"
console.log(typeOf({}));  //Return string "object"
console.log(typeOf(null));  //Return string "null"
console.log(typeOf(undefined));  //Return string "undefined"

In JavaScript, function is a special structure. It can be a code set or a data type; It can be used as an object or as a constructor to create a type. The usage of JavaScript functions is flexible, which is also a manifestation of the agility of JavaScript language (functional programming).

Two simple value types -- Boolean and Null,

Boolean

A Boolean contains only two fixed values: true and false. Where true stands for "true" and "false" stands for "false".

In JavaScript, the six special values of undefined, null, "", 0, NaN , and , false are called false when they are converted to Boolean values. Except for false values, any other type of data is true when converted to Boolean values.
Example
Use the Boolean() function to cast a value to a Boolean value.

console.log(Boolean(0));  //Return false
console.log(Boolean(NaN)); //Return false
console.log(Boolean(null)); //Return false
console.log(Boolean("")); //Return false
console.log(Boolean(undefined)); //Return false

Null

Null type has only one value, namely null, which represents a null value and defines a null object pointer.

Use the} typeof} operator to detect the} null} value and return} Object, indicating that it belongs to the Object type, but JavaScript classifies it as a special type of value.

Set the initialization value of the variable to null. You can define an alternate empty object, that is, a special object value, or a non object. For example, if an object is detected to be empty, it can be initialized.

if (men == null){
    men = {
        //Initialize men
    }
}

Object object

The object of JavaScript is an unordered collection data type, which is composed of several key value pairs.
JavaScript objects are used to describe an object in the real world. For example, in order to describe the naughty child "Xiao Ming", we can use several key value pairs to describe him:

var xiaoming = {
    name: 'Xiao Ming',
    birth: 1990,
    school: 'No.1 Middle School',
    height: 1.70,
    weight: 65,
    score: null
};

JavaScript uses a {...} Represents an object. Key value pairs are declared in the form of xxx: xxx, separated by. Note that the last key value pair does not need to be added at the end. If it is added, some browsers (such as lower versions of IE) will report an error.
The above object declares a name attribute, the value is xiaoming, the birth attribute, the value is 1990, and some other attributes. Finally, after assigning this object to the variable xiaoming, you can obtain xiaoming's attributes through the variable xiaoming:

xiaoming.name; // 'Xiao Ming'
xiaoming.birth; // 1990

Access properties through Operator, but this requires that the property name must be a valid variable name. If the property name contains special characters, it must be enclosed with '':

var xiaohong = {
    name: 'Xiao Hong',
    'middle-school': 'No.1 Middle School'
};

xiaohong , attribute name , middle school is not a valid variable, so it needs to be enclosed with ''. Access to this property is also unavailable Operator, which must be accessed with ['xxx ']:

xiaohong['middle-school']; // 'No.1 Middle School'
xiaohong['name']; // 'little red'
xiaohong.name; // 'little red'

You can also use xiaohong['name '] to access xiaohong's name attribute, but xiaohong Name is more concise. When we write JavaScript code, we try to use standard variable names for attribute names, so that we can directly use object Access a property in the form of prop.
In fact, all properties of JavaScript objects are strings, but the value corresponding to the property can be any data type.
What will be returned if you access a non-existent property? JavaScript stipulates that accessing nonexistent attributes does not report an error, but returns undefined:

'use strict';
var xiaoming = {
    name: 'Xiao Ming'
};

Because JavaScript objects are dynamically typed, you can freely add or delete attributes to an object:

var xiaoming = {
    name: 'Xiao Ming'
};
xiaoming.age; // undefined
xiaoming.age = 18; // Add an age attribute
xiaoming.age; // 18
delete xiaoming.age; // Delete age attribute
xiaoming.age; // undefined
delete xiaoming['name']; // Delete name attribute
xiaoming.name; // undefined
delete xiaoming.school; // Deleting a nonexistent school attribute will not report an error

If we want to check whether "xiaoming" has a certain attribute, we can use the "in" operator:

var xiaoming = {
    name: 'Xiao Ming',
    birth: 1990,
    school: 'No.1 Middle School',
    height: 1.70,
    weight: 65,
    score: null
};
'name' in xiaoming; // true
'grade' in xiaoming; // false

However, be careful. If in judges that an attribute exists, it may not be xiaoming's attribute. It may be inherited by xiaoming:

toString' in xiaoming; // true

Because toString is defined in the object object, and all objects will eventually point to the object on the prototype chain, xiaoming also has the toString attribute.
To determine whether a property is owned by xiaoming rather than inherited, you can use the {hasOwnProperty() method:

var xiaoming = {
    name: 'Xiao Ming'
};
xiaoming.hasOwnProperty('name'); // true
xiaoming.hasOwnProperty('toString'); // false

Topics: Javascript