JavaScript Basics (data type)

Source: Internet
Author: User
Tags logical operators object object string to number

Data typeBoolean: True/fasle
Console.log (typeoftrue); // "Boolean"

Number:true-->1 false-->0

Console.log (Number (true)); Console.log (number (false));

Boolean: Converts other data types to Boolean values;

Console.log (Boolean (12));//trueConsole.log (Boolean (10));//trueConsole.log (Boolean (0));//falseConsole.log (Boolean ("{}"));//trueConsole.log (Boolean ("12px"));//trueConsole.log (Boolean ([]));//trueConsole.log (Boolean ({}));//trueConsole.log (Boolean (NULL));//falseConsole.log (Boolean (undefined));//falseConsole.log (Boolean (NaN));//falseConsole.log (Boolean (/\d/));//true

Other data type to Boolean type is false with only five values: 0 "" NaN null undefined;

logical operators: | | : or &&: and;! Take counter 1. Place in the IF condition;
| |: As long as one of them is true, the overall result is true;&&: As long as there is a false, the overall result is false;
if ("" || 0) {console.log}if (&& 0) {console.log (0);}

2. The function of assigning value;

if the previous value of the Boolean is true, take the preceding value directly, and if the turn Boolean is false, take the value directly behind it;
var abc = "12PX" | | Undefined;console.log (ABC);//12PX
If the forward Boolean is true, take the following value directly, or if the Boolean is false, directly preceding the value;
var null &&N; console.log (BCD);//null

Comparison between data types: true/false;

!: take the reverse; first, the value of the following is converted to a Boolean value and then reversed; Console.log (!1);//true!!: Directly converts the current Boolean value Console.log (!! "); /false
null and undefined
null and undefined all mean no, typeof null–> "Object"; null object pointer; difference: null means no now, but may be later; undefined: Not now, not later;
Common NULL Scenarios
    1. 1. Gets the element by ID and returns NULL if the ID name does not exist;
    2. 2. When capturing through a regular, the result is null if the content is not captured;
    3. 3. When the object is emptied and the heap memory is destroyed, the assignment is null when the object is assigned a value;
a common undefined situation
    1. If the property name does not exist when the property value corresponding to the object property name is obtained, then the return value is undefined;
    2. The parameter of the function is stored undefined in the function if there is no argument assignment.
    3. If the function does not return, then the return value of the function is undefined;
    4. If the variable is only declared and not assigned, then the default storage is undefined;
var gg = document.getElementById ("AB"); Console.log (GG); // NULL var obj = {a:1};console.log (obj.b); // undefined var C;console.log (c); // undefined var A;console.log (a); // undefined

Object

1. Reference data type, stored in heap memory; Object : Consists of key-value pairs; key-value pairs consist of property and attribute names; key-value pairs are separated by commas, and attribute names are delimited by colons; property names are string types, property values are one of the data types; var obj = {a:[]} var o = {A:2} has no length attribute; no index; Console.log (obj[0]); var a = 1;
definition of Object
1. First, a new space address will be opened; the space address is 16 binary; 0-9a-f; 2. Store the key value pairs in the current heap memory; 3. Give this space address to the object name;
//the object name actually stores a space address;console.log (obj);varA = {};varb = {};//in JS encountered {}, [] will open up a new space address;varA = b = {};varA = b = c = d =10;d= 9; Console.log (a);//Ten;varA = b = c = {a:1};c={};C.A= 2; Console.log (a);//{a:1}Console.log (c);//{a:2}

Search and delete of objects

varobj = {num:1,str: "abc", 6:10};//1. New key-value pairs://The object name. Property Name = property valueOBJ.A = 100; Console.log (obj);//object Name [' property name '] = attribute value;Obj["a"] = 100; obj["12"] = 13; Console.log (obj);//If the attribute name is a number, it is displayed in the order of the numbers from small to large;//2. Query: If the property name does not exist, then the property value obtained is undefined;varv =Obj.num;varb = obj["num"];console.log (v);//1varc = "num";varD =Obj[c];varobj = {num:1,str: "abc", 6:10};//3. Modify: Property is not new, there is a modification;Obj.num = 10; OBJ.A= "1"; obj["Num"] =100; Console.log (obj);//4. Delete//false deletion;//true DeleteObj.num =NULL//Delete Delete Object name. Property nameDeleteobj.num;console.log (obj);varA =Obj.num;//in: Determines whether the current property name exists in the object, and returns True if there is no return false;//property name in Object nameConsole.log ("Num1"inchOBJ);

Array

array: Consists of array members separated by commas, indexed, with length, var ary = [12,12,47,128, "UU", {},true]; array members have a corresponding index; starting from 0 ; Has the length property, which represents the number of array members;
Console.log (ary[4]); Console.log (ary.length); // 7Console.log (ary[ary.length-1]); var a = []; var b = []; ary[7] =console.log (ary);

Detection

typeof: Returns a string in which the contents of the string represent the current data type;typeof NaN--and "number" typeof null--and "object" null object pointer
Limitations: Only the basic data types can be distinguished, but cannot subdivide objects under the object data type, arrays, regular;typeof true–> "boolean" typeof undefined–> "undefined"
instanceof: A method that detects whether the current instance belongs to a class;
var pbj = {a:1}; var aru =instanceofinstanceofinstanceof Array);

Constructor: detects the constructor of the current instance Object.prototype.toString.call ([]); the most accurate way;
The rule of comparison between data types=: assigned value;
var a = +; var a =; var obj == 100;
= =: compare; return a Boolean value;
Console.log (1 = = "1");//trueConsole.log (1 = =true);//trueConsole.log ([] = = "1");//falseConsole.log ({} = = []);//falseConsole.log (nan = = Nan);//falseConsole.log (NULL==NULL);//trueConsole.log (NULL= = undefined);//trueConsole.log (NULL= = = undefined);//false
*****nan and himself are not equal * * * *The law of data type comparison
    • Object = = object: The space address
    • Object = = String object is called by default on the ToString method, preceded by a string, and then compared
    • object = = Boolean: Object first to String, then to number, Boolean to direct to number;
    • Object = = Number: The object first calls ToString to convert to a string, and then to numbers;
    • Boolean = = Number: Boolean to number;
    • String = = Number: string to number;
    • Boolean = = string: A Boolean to a number, a string to a number, and then a comparison;
    • NULL = = Undefined:true;
    • null and undefined and other data type comparisons are false;
 console.log ({}=={}) //  false  Console.log ({}== ""); //  false  Console.log ({} = = true ); //  false  Console.log ([] = = 0); //  console.log ([]==false ) //  true  Console.log (true  = = 10); // false  console.log ("" = = 0); //trueconsole.log (true = =  "1"  "1" = = "1px"  "= =" ");//false 

! : First, the value of the back is converted to a Boolean value and then reversed;

false); // true false); // true; Console.log (!isnan ("12px") = = 0); // true; Console.log (!{} = = {}); // false ({}). toString () "[Object Object]"
= = = Absolute comparison; no conversion between data types; As long as the data type is different, it is false;
Console.log (1 = = = "1"); // falseconsole.log ({} = = = {}); // false false); // true
!: followed by an equal sign, which is not equal to,!==: Absolute not equal to console.log ([]!== false)//true! =: The comparison between data types is performed;

JavaScript Basics (data type)

Related Article

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.