JS//Variable type and calculation

Insert picture description here

JS-variable types and calculations

1 knowledge points

1.1 Variable types

Value type VS reference type

Value type: Number / String / Boolean / Undefined / Null
Reference type:Object / Array / Function

Detailed explanation of typeof operator

number(NaN)、string、boolean、undefined、object(object/array/null)、function

typeof 123  //number
typeof 'abc'  //string
typeof true  //boolean
typeof undefined  //undefined
typeof null  //object
typeof { }  //object
typeof [ ]  //object
typeof console.log  //function

(1) Undefined and null are equal; (2) NaN is not equal to any value, nor is it equal to itself.

1.2 Variable calculation-forced type conversion

String splicing

var a = 100 + 10  //110
var b = 100 + '10'  //10010

== operator

100 == '100'  //true
0 == ' '  //true
null == undefined  //true

(1) Undefined and null are equal;
(2) NaN is not equal to any value, nor is it equal to itself.

if statement

var a = true;
if (a) {
    
      }    //a为true
var b = 100;
if (b) {
    
      }    //b为true
var c = ' ';
if (c) {
    
      }    //c为false

false, null, undefined, "", 0, NaN-judged as "fasle", the rest are "true"

logic operation

10 && 0  //0
' ' || 'abc'  //'abc'
!window.abc  //true

判断一个变量会被当做 true 还是 false
var a = 100
console.log(!!a)

2 Q&A

Topic:
*Types that can be obtained by using typeof in JS
*When to use === When to use ==
*What are the built-in functions in
JS*What types of JS variables are classified according to the storage method, and describe their characteristics
*How to understand JSON

2.1 Types that can be obtained using typeof in JS

number / string /boolean / undefined / object / function
(number)/(string)/(ture.false)/(undefined)/(null.object.array)/(funtion)

2.2 When to use === When to use ==

===Strictly equal, will compare the types and values ​​of two values.
==Abstractly equal. When comparing, the type conversion will be performed first, and then the values ​​will be compared.

– Only use == in the following cases, use === in other cases:

if (obj.a == null) {
    
    
    // 这里相当于 obj.a === null || obj.a === undefined ,简写形式
    // 这是 jquery 源码中推荐的写法
}

2.3 What are the built-in functions in JS

Number / String / Boolean / Object / Array / Function /
Date / RegExp / Error

2.4 What types of JS variables are classified according to the storage method, and describe their characteristics

  • Distinguished into-value type, reference type;
    value type: Number / String / Boolean / Undefined / Null
    reference type: Object / Array / Function
  • Differences:
    ①The storage location is different.
    Value type: It occupies a fixed space and is saved in the stack. The value itself is saved and copied. Typeof() can be used to detect the value type;
    reference type: The occupied space is not fixed, and it is saved in the heap. The copy is a pointer to the object, you need to use instanceof() to detect the data type, and the object constructed by the new method is a reference type;

    ②The
    direct assignment of a variable with a
    different value type is a deep copy; a direct assignment of a reference type variable It is a passing reference, it is a shallow copy;
    ③Can you add attributes and method
    value types can not, but reference types can;

2.5 How to understand JSON

JSON is a JS object

There are two methods:
①stringify: JSON.stringify converts json objects into strings
②parse: JSON.parse parses strings into json objects

JSON is also a data exchange format

Guess you like

Origin blog.csdn.net/weixin_37877794/article/details/114166639