[Front-end Interview] Variable and Type Calculation

1. Topic

  • 1. what types can 1.JS get by using typeof
  • Choice of = = = and = =
  • What are the built-in functions in JS
  • JS variables are divided into which types according to storage methods, and their characteristics are described
  • How to Understand JSON

2. Knowledge points

2.1 Value Types and Reference Types

Value type (boolean, string, number, null, undefined)

var a = 10;
 var b = a;
 a = 20;
 console.log(b);  //10

Reference Type (Object, Array, Function)

var a = {x:10}
 var b = a;
 a.x = 20;
 console.log(b);  //20

Value types store values directly in the heap. Assigning A to B in memory opens up a new space for B to store the same value.

Reference types are stored in two blocks. First, an actual value is stored in the heap, and then a reference address of the value in the heap is stored in the stack, pointing to the objects in the heap. If A is assigned to B, is it to re-open a space in the stack or is it the reference address of the same object? A and B have the same address and point to the same object. When the object value changes, both will change at the same time.

The value of the reference type is generally relatively large, which can save memory space.

2.2 typeof operator

typeof 'abc' //string
 typeof 123  //number
 typeof true  //boolean
 typeof undefined  //undefined
 typeof null  //object
 typeof {a:10}  //object
 typeof [1,2,3]  //object
 typeof console.log()  //function

2.3 Type Conversion

Strong type conversion: cast by String (), Number (), Boolean (), parseInt () function

Scenarios where implicit type conversion may occur

  • String concatenation
  • Use = =
  • if statement
  • Logical cycle

First, look at whether there is NaN before and after the double equal sign. If there is NaN, it will all return false.

Second, look at whether there is boolean before and after the double equal sign. if there is boolean, boolean will be converted into numbers. (false is 0, true is 1)

Three, then look at the double equal sign before and after the string, there are three kinds of situations:

1. The other party is an object, and the object is converted using toString () or valueOf ().
2, the other is a number, string to number; (Examples have been given earlier)
3, the other is a string, direct comparison;
4. Other returns false
Four, if it is a number, the other is an object, the object take valueOf () or toString (), the other will return false

Five, null, undefined will not type conversion, but they are both equal

The above conversion sequence must be kept in mind that during the interview, there are often types of problems.

100'==100 // converted to string
 '' ==0 // converted to false
 undefined == null;  // true
 1 == true;  // true
 2 == true;  // false
 0 == false;  // true
 0 == ' ';  // true
 NaN == NaN;  // false
 [] == false;  // true
 [] == !  [];  // true
//converted to false in if:
10 && 0 //0 10 to true
'' || 'abc' //abc'' converted to false
!  window.abc  //true

2.4 the difference between null and undefined

Null: it is an assigned object. it deliberately assigns an object to null, which means it is empty and should not have a value. therefore, it is normal for an object to be null. typeof null returns’ object’, and null can be converted to 0

Undefined means “missing value”, that is, there should be a value here, but it has not been defined yet; Returns’ undefined’ for NaN (a special value other than a numeric value) typeof undefined when converting to a numeric value

3. Answers to questions

3.1 what types can js obtain using typeof

typeof 'abc' //string
typeof 123  //number
typeof true  //boolean
typeof undefined  //undefined
typeof null  //object
typeof {a:10}  //object
typeof [1,2,3]  //object
typeof console.log()  //function

3.2 === and = = options

Jquery source code in writing:

Use all but the following = = =

if(obj.a == null){
 //equivalent to obj.a = = = undefined | | obj.a = = = null

What are the built-in functions in 3.3 JS

As a language, node and browser web are not considered.

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

Built-in objects: Math,JSON

3.4 JS variables are divided into which types according to storage methods, and their characteristics are described

What reference type is the value type

3.5 How to Understand JSON

JSON is a built-in object in JS


  • JS object {x:10}
  • JSON object {‘x’:10}
  • JSON string “{‘x’:10}”
//Convert JS objects into json strings
 //Convert json string into json object

3.6 Strict Mode


Eliminate some unreasonable and imprecise aspects of Javascript syntax and reduce some weird behaviors.
Eliminate some unsafe aspects of code operation to ensure the safety of code operation;

Improve compiler efficiency and speed;

This will pave the way for the new version of Javascript in the future.


"use strict";

You can choose to place it in a function or in a custom scope.

This is prohibited from pointing to global objects

function f(){
    return !  this;
//return false because "this" points to a global object, "!  This "is false.
  function f(){
    "use strict";
    return !  this;
//Returns true because in strict mode, this value is undefined, so "!  This "is true.

Create eval scope

In normal mode, Javascript language has two variable scopes: global scope and function scope. Strict mode creates a third scope: eval scope.
In normal mode, the scope of eval statement depends on whether it is in global scope or function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables. The variables it generates can only be used inside eval.

"use strict";
   var x = 2;
   console.info(eval("var x = 5;   x"));  // 5
   console.info(x);  // 2

Explicit declaration of global variables

v = 1;  //Error reported, V not declared
   for(i = 0;   i < 2;  I++) {// reported an error, I did not declare

Deleting variables is prohibited

Variables cannot be deleted in strict mode. Only an object property with configurable set to true can be deleted.

"use strict";
   var x;
   delete x;  //syntax error
   var o = Object.create(null, {'x': {
       value: 1,
       configurable: true
   delete o.x;  //Delete succeeded

Functions cannot have parameters with duplicate names

Reserved word

In order to transition to the new version of Javascript in the future, some reserved words have been added to the strict mode: Implements, Interface, Let, Package, Private, Protected, Public, Static, Yield.
Using these words as variable names will report an error.

Function package(protected) {// syntax error
     "use strict";
     var implements;  //syntax error

3.7 eval

1. There is no application scenario that must be used

2. It is not easy to debug and is not readable.

3. If you use eval in the old browser, the performance will drop 10 times.

4. easy xss