Understanding basic types of JavaScript

The development history of js is not repeated. After all, the information is easy to find. Baidu Encyclopedia can also just understand its history. There is no need to recite it.

JavaScript is a lightweight programming language, which can be inserted into HTML pages and executed by all browsers (there is a js running environment in the browser kernel).

Add: the most common are the five browsers and the four cores, which do not contain any Chinese browsers, because Chinese browsers use other people's cores. You can see the specific content details

For the use of a language, some people certainly need to understand some of its common types.

The big data aspect of JavaScript can be divided into two types: basic types (value types) and reference data types.

  • Basic types: string, number, Boolean, null, undefined, symbol
  • Reference data types: object, array, function.

character string

String is an unavoidable data type in any language. Although the name of JavaScript carries java, the definition of string is much worse.

Strings in JavaScript can be enclosed in double or single quotes.

var a='dfd';
var a="ddf"

/Both declarations are strings

number

If you can divide positive and floating-point data in java, they are both of the same type in JavaScript.

var num1=34.00;
var num2=34;
// Scientific counting can be used
var num3=1e2; //100
var num4=le-2;//0.01

Boolean

This has only two values, true and false

var x=true;
var y=false;

array

Array objects use separate variable names to store a series of values. Unlike java, this can store numbers and strings at the same time.

var arr=[1,'a']
perhaps
var arr=new Array(2);
arr[0]=1;
arr[1]='a';
perhaps
var arr=new Array(1,'a');


// And the length is not just 2.
arr[2]=123;
//The arr length is 3

//Because the subscript of the number starts from 0, the stored value can be obtained through arr [subscript]

object

Objects are separated by curly braces. Inside the parentheses, the attributes of the object are defined in the form of a name and value pair (Name: value). Attributes are separated by commas

var car={
    color:"red"
    brand:"haval"
}

// Get the color of the car
var color=car.color;Or var color=car["color"];

null and undefined

In JavaScript, null means "nothing". Null is a special type with only one value, representing an empty object reference. undefined is a variable that has no set value.

It seems that there is a difference between null and undefined.

var a;
console.log(a);// Print a on the console
//The output is
undefined

This shows that if a variable is declared but not assigned, its value is undefined.

Next, a typeof method is added to detect the type of object.

typeof operator

Type of operator to detect the data type of the variable.

typeof "a"                // Return string
typeof 3                  // Return number
typeof false                 // Return boolean
typeof [1,2,3,4]             // Return object
typeof {color:'red', brank:"haval"} // Return object

// Any of the above (typeof data)
typeof  typeof Any data type   //"string"  

//typeof any data type converts the data type to a string and returns

Now look at null and undefined:

console.log(typeof null); //Output object type
console.log(typeof undefined);//Output undefined

It can be seen that null is the object type and undefined is undefined.

Add an operator = = and = = =.

First declare a variable a=1;

operatordescribecompareReturn value
==be equal tox1 x'1' x==2The returned values are true, true, false
===Absolute equal (value and type are equal)x==="5" x===5The returned value is false and true

It can be seen that the value is automatically converted (in addition to numbers, string objects can be compared, etc.) and = its value and type must be equal.

null === undefined           // false
null == undefined            // true

This shows that the two values are equal, but their types are not equal.

null means "no object", that is, there should be no value there. Typical usage is:

(1) As a parameter of a function, it means that the parameter of the function is not an object.

(2) As the end of the object prototype chain.

undefined means "missing value", that is, there should be a value here, but it has not been defined. Typical usage is:

(1) When a variable is declared but not assigned, it is equal to undefined.

(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.

(3) The object has no assigned property, and the value of the property is undefined.

(4) When the function does not return a value, it returns undefined by default.

In addition:

Number(null) //0

Number(undefined) //NaN 

The NaN property is a special value representing a non numeric value. This property is used to indicate that a value is not a Number. You can set the Number object to this value to indicate that it is not a numeric value.

supplement

You can use the keyword "new" to declare its type in advance

var t_String=new String;
var t_Number=      new Number;
var t_Boolean=      new Boolean;
var t_Array=   new Array;
var t_car= new Object;

Tags: Javascript

Posted on Fri, 08 Oct 2021 14:36:11 -0400 by EvoDan