This is the first day of my participation in the August More Text challenge

Js’s upper application is endless, but the regression source is based on JS. So when learning other knowledge points, want to review js knowledge points. As the saying goes, “The foundation is not firm, and the earth shakes.”

First JS application

Remember hello World when you wrote your first JS? This is where the door to the program starts.

document.write('hello world! ');
Copy the code

The effect is to output a Hello World in the page page

Tips: javascript is case-sensitive, and the same word in different case means different things.

Variable declarations

In the variable declaration, I’m sure you remember. All js must be declared when used. That is, to create a space in the memory address. And wait for the data to “check in.”

way

There are three ways to declare variables: var, let, and const

  • varDeclared variable, defined variable will be placed on the window of the global variable.
  • letDeclare a local variable
  • constDeclare a constant

grammar

// Declare a single variable
varThe variable name.letThe variable name.constThe variable name.// Declare multiple variables
varThe variable name1The variable name2The variable name3The variable name4;
letThe variable name1The variable name2The variable name3The variable name4;
constThe variable name1The variable name2The variable name3The variable name4;
Copy the code

Please check the similarities and differences of the three

Grammar notes

Syntax comments are divided into single-line comments and multi-line comments.

Vscode is used as the development tool below:

Use CTRL + / to type a single line comment// xxxxUse Alt + shit + a to type multi-line comments/* { a: 1, b: 2 }, */
Copy the code

The following uses Webstorm as the development tool:

Use CTRL + / to type a single line comment// xxxxUse CTRL + shit + / to type a multi-line comment/* { a: 1, b: 2 }, */
Copy the code

The global variable

Global variables default to properties of a global object (window), and are often set and accessed using the window.variable syntax. However, we can usually omit the window and use the corresponding variables, such as alert, console, etc

  • Among the ES5, attributes of the top-level object are equivalent to global variables (the top-level object in the browser environment iswindow, is in the NodeglobalObject);
  • Among the ES6.var/functionDeclared global variables are still properties of the top-level object, butlet/const/classDeclared global variables are not attributes of the top-level object, that is, starting with ES6, global variables and attributes of the top-level object are separated.
// ES5
var a = 'abc';
window.a; // 'abc'

// ES6
let b = 'abc';
window.b; // undefined
Copy the code

The data type

Js data types are divided into primitive types and reference types

The original type

Raw values are stored in a way that memory creates a place to store the current value of the original value.

  • 1.Boolean: Boolean value,trueandfalse;
  • 2. Null: case sensitive (i.enull/Null/NULLTotally different);
  • 3. Undefined: null type, undefined value;
  • 4.Number: indicates the value type, for example100;
  • 5.String: a String, such as’ Hello world’.
  • 6.Symbol (ES6 added) : represents a unique and immutable data; Details see

Reference types

Reference values are stored in such a way that memory needs to open up extra memory space to hold data, and the current variable holds the address of extra memory.

    1. Object: the Object,{key: value}In the form of
    1. -Leonard: Function,function () {xxx}In the form of

Get data type

There are several ways to get the data type of a variable in JS:

typeof

You can judge the basic data types in JS, but not the specific types of objects

grammar

typeofvariableCopy the code
var a = 1;
console.log(typeof a) // number
Copy the code

Common methods for determining base types

function basicTypeof(cc){
    return cc === null  ?"null": typrof(cc); }Copy the code

Object.prototype.toString.call(1)

You can determine specific object types, including re types, but you cannot determine custom object types.

grammar

 Object.prototype.toString.call(variable) 
Copy the code
const a = 1;
console.log(Object.prototye.toString.call(a)) // [object number]
Copy the code

instanceof

Only the specific type of an object can be determined, but it can be used to determine a custom object type

grammar

variable instanceoftypeCopy the code

Boolean is returned

constructor

Each instance of object has the attribute constructor, which holds the function used to create the current object

Common special characters

character meaning
\b Back space
\f Page identifier
\n A newline
\r A carriage return
\t Tab
\v Vertical TAB character
' Single quotes
" Double quotation marks
\ Backslash character (\)