Introduction to JavaScript syntax

Keywords: Javascript

This blog is a personal self-study record. If there is any deficiency, please criticize and correct it.

Version: ES 6

The basic syntax in this chapter refers to the syntax commonly used in JS

Expressions and statements


  • The value of the 1 + 2 expression is 3
  • The value of the add(1,2) expression is the return value of the function
  • The value of the console.log expression is the function itself
  • What is the value of the console.log(3) expression? Answer: undefined


  • var a = 1 is a statement whose value is undefined


  • Expressions generally have values, and statements may or may not
  • Statements are generally used to change the environment (declaration, assignment)
  • The above two sentences are not absolute, but in general

Case sensitive

  • var A and var A are different
  • Object and object are different
  • Function and function are different

Spaces and carriage returns

As long as it does not affect sentence breaks, most of the spaces are meaningless

  • There is no difference between var a = 1 and var a = 1


  • Ibid., no difference
  • The only place where carriage return cannot be added is after return (if carriage return is added after return, the return value is undefined)


The variable name is an identifier


  • The first character: can be letters, $_ Or Chinese
  • The following characters: in addition to the above, they can also be numbers


// Single-Line Comments 

   multiline comment 

Annotation classification

The more notes you write, the better

Bad notes

  • Translate the code into Chinese
  • Obsolete comments
  • Notes to vent dissatisfaction

Good note

  • Notes to important information
  • Footwell notes
  • Why is the code written strangely and what bug s do you encounter

Block block

  • Is to wrap the code together with {}
  let a = 1
  let b = 2
  • Often used with if / for / while

if statement

If... Then


if (condition) {
  // Statement 1
  else {
    // Statement 2
  • {} can be omitted when there is only one sentence (this is not recommended)

Abnormal condition

  • The expression can be very abnormal, such as a = 1
let a = 2;
if (a = 1) {
    console.log("a Yes 1");

// Output: a is 1
// Because = is the assignment symbol, the assignment symbol can be used in the expression. If it is judged to be used===
  • Statement 1 can be very abnormal, such as nested if else
  • Statement 2 can be very abnormal, such as nested if else
  • Indent:
a = 1;
if(a === 2)
	console.log('a Equal to 2')

// a equals 2

// The above code is equal to:

a = 1;
if(a === 2) {
console.log('a Equal to 2')

switch statement

if... else... Upgrade


switch(expression) {
    case value1:
        sentence	// Execute the statement here when the result of expression matches value1
    case value2:
        sentence	// Execute the statement here when expression matches value2
        sentence	// If the expression does not match the value above, execute the statement here

Conditional operator?:

Question mark colon expression

Conditional (ternary) operator

Replace if else


condition ? exprIfTrue : exprIfFalse

Short circuit logic & &||

Instead of if or if else


And short circuit logic


expr1 && expr2 // If expr1 is true, expr2 is returned; otherwise, expr1 is returned


Or short circuit logic


expr1 || expr2 // If expr1 is true, expr1 is returned; otherwise, expr2 is returned

while loop



while (condition) {
	sentence	// When the condition is true, execute the statement here. After execution, judge whether the condition is true or false again 

Initialization variables, judgment conditions, loop body, self increment or self decrement are required
var a = 1;		// initialization
while(a <= 10) {	// judge
	console.log(a);	// Circulatory body
      a = a + 1;	// Self increasing

for loop


for(initialization;Judgment conditions;Self increasing or self decreasing) {
  sentence	// Loop body, after executing the statement here, execute self increment or self decrement, then judge, and then execute the statement here

break and continue

Exit all loops and skip the current loop

label statement

JavaScript language allows you to have a label in front of the statement, which is equivalent to a locator. It is used to jump to any position of the program. The format of the label is as follows.


The label can be any identifier, but it cannot be a reserved word. The statement part can be any statement.

Tags are usually used with break and continue statements to jump out of a specific loop.

foo: {
  break foo;
  console.log('This line will not output');
// 1
// 2

Posted by cyberrate on Sun, 24 Oct 2021 12:16:11 -0700