The data type in JS

Source: Internet
Author: User

The data types in JS are divided into basic data types and reference data types;

The basic data types include number, string, Boolean, null, undefined;

Reference data types include object data types objects and function data type functions

The base data type is manipulated by value, and the reference data type is manipulated by a reference address.

    1. Number (numeric)
      • Number contains positive, negative, 0, decimal, NaN (Nan:not a number)
      • Three cases of equal sign in JS
        • = assigned value
        • = = comparison, is to determine whether the left and right sides of the value is equal
        • = = = Absolute comparison, is to determine whether the left and right sides of the value is absolutely equal
      • Nan==nan--"false

 The method in number

  method One:IsNaN (the value to be detected): is not a number

Function: Detects whether a value is a valid number, if it is not a valid number, returns True if it is a valid number, returns false;

    Console.log (IsNaN (123));  // false    Console.log (IsNaN (' haha '));  // true

  method Two : Cast: Number (the value to be converted)--"Convert other data types to numbers data type

If the value to be detected is a different data type, the default number () is converted to numbers, and then the

  method Three: non-forced conversion:parseint/parsefloat

parseint (the content to be converted); Search from left to right, whenever you encounter a character that is not a number, stop looking; if it is a string, the first character in the string is not a number and directly returns Nan

  parseint (' 12px ');  //  A

  Parsefloat and parseint, just can identify a point more

    parsefloat (' 12.34px ');  // 12.34    parsefloat (' 12.34.5px ');  // 12.34    parsefloat (' 12a3c ');  //  a    parsefloat (' 1a2.3 ');  // 1    parsefloat (' a12.4px ');  // NaN    parsefloat (' 12a12.5px '); //  A

Note:    the common denominator of casts and non-casts: return nan directly as long as the first character is not a valid number

method Four:toFixed (n) Keep n digits after the decimal point

var num = 3.141592654; Console.log (Num.tofixed (2)); // ->3.14

2.boolean:  only two values of true false

Rule: 0 NaN ' null undefined these five values are false and the rest are true 

Execute a boolean () 、!、!! all three methods convert other data types to Boolean types

! An exclamation mark is reversed (this value is first converted to a Boolean type "execute Boolean ();", and then reversed)

!! Inverse inversion is equivalent to converting directly to a Boolean type and executing the Boolean () method

If there is only a single value in the conditional judgment, it means: First convert this value to a Boolean type, and then see if it is true or false, yes true words condition is set up, the condition is not established

3.null and undefined

Null means "No object", that is, there should be no value at all. Typical uses are:

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

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

Object.getprototypeof (Object.prototype)//  null

Undefined means "missing value", that is, there should be a value here, but there is no definition. Typical uses are:

(1) The variable is declared, but when it is not assigned, it is equal to undefined.

(2) when calling a function, the argument that should be supplied is not provided, which equals undefined.

(3) The object does not have an assigned property, and the value of this property is undefined.

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

var//  undefinedfunction//  undefinedvar   new//  undefinedvar x =// undefined

Data type in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.