Detailed explanation of JavaScript basics

   This article summarizes the basic knowledge of JS, including basic syntax, data types and variables, operations, strings, arrays, objects, conditional judgments, loops, Map and Set, iterable and other knowledge points. See below for details

 

(1) Expansion

   Reference: First, let’s talk about JS references, two ways: embedded and external calls

   Editor: Sublime Text and WebStorm, PyCharm recommended

 

(2) Basic grammar

   ①The colon ends with {}

   The syntax is similar to the Java language, each statement ends with ;, and the statement block uses {...}

Although JavaScript does not require adding ; at the end of each statement (the browser engine will automatically add it), it is recommended not to omit it during the development process. Because letting the browser engine automatically add semicolons will change the semantics of the program in some cases, resulting in inconsistent results with expectations

   ②Indent

   Note that statements within curly braces {...} are indented, usually 4 spaces. Indentation is not required by JavaScript syntax, but indentation helps us understand the level of the code, so we must follow the indentation rules when writing code. Many text editors have an "auto-indent" feature (such as WebStorm's Ctrl+Alt+L) to help organize code

   ③ Notes

   Line comments and block comments (comments are for developers to see and are automatically ignored by the JavaScript engine)

Line Comments: Characters starting with // until the end of the line are considered line comments

// this is a line comment

Block comments: wrap multi-line characters with /*...*/ to treat a large "block" as a comment

/* From here are block comments
still a note
still a note
end of comment */

   ④ upper and lower case

JavaScript is strictly case-sensitive. If the case is wrong, the program will report an error or not work properly

 

(3) Data types and variables

 ①Data type

Divided into two categories: simple data and complex data

There are five kinds of simple data: undefined, null, boolean, number and string; there is only one kind of complex data, namely object.

   Simple data types:

[1] undefined : Indicates that the variable does not contain a value;

[2] null : Indicates that the variable is empty;

[3] boolean : true or false represents a boolean value;

[4] number : js has only one type of number, the number can be with or without a decimal point

[5] srting : A string is a carrier of characters and must be surrounded by pairs of quotation marks

   Note: typeof is an operator provided by javascript. After running, it will return the data type of the data in the brackets

             If the return result of typeof(3) is 'number', the type of 'hello' is number

 

(4) Operation

①Commonly used operation symbols

   Arithmetic operators: + plus sign, - minus sign, / division sign, * multiplication sign, % remainder, ++ accumulation, -- decrement

   Logical operators: && and, || or, ! No

   Comparison operators: == , === , != , > , < , >= , <= 

Notice:

   ① When JavaScript was designed, there are two comparison operators:

The first is the == comparison, which will automatically convert the data type and then compare, and in many cases, will get very strange results;

The second is === comparison, it will not automatically convert the data type, if the data type is inconsistent, return false, if the same, then compare

E.g:

if('1'==1){
      alert('same')
}else{
      alert('different')
}

 Because == is used to judge, the data type will be converted, that is, 'same' will be prompted here

Recommendation: Due to this design flaw in JavaScript, it is recommended not to use == comparison, always stick to === comparison

   ②NaN This special Number is not equal to all other values, including itself. For the detailed explanation, I will summarize it in the detailed explanation of NAN in JavaScript in the following article.

 

(7) String

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326102743&siteId=291194637