Preface

Worked as a lecturer for a while and taught computer courses for two years. A former Python teacher and developer, he turned to JavaScript to find work.

With this blog post, JavaScript learning begins.

1. Basic knowledge literacy

  • Expression: or value. For example,1 + 2,3,'and'And so on are expressions. In other words, on the right-hand side of the equal sign, the value given to the variable is the expression, but strictly speaking, the expression is broader than the value of the variable.
  • Statement: a variable + an expression is. Or you can just define an empty variable.

JavaScript is a dynamic language and there is no need to specify variable data types in advance.

2. What are identifiers and rules for identifiers

An identifier is a terminology for a name, including variable names, function names, parameter names, attribute names, and so on.

There are several rules for identifiers:

  • The first character must be a letter, an underscore (_), or a $(dollar sign);
  • Unicode characters are supported, and Chinese characters are also supported (but not used), but only letters, numbers, and underscores (_) are recommended. In special cases, $is allowed.
  • The name cannot be the same as a JavaScript keyword or reserved word. In short, English words that are most relevant to computers and the programming language itself are most likely to be reserved, but words like apple or banana are fine. Below is a list of reserved words that comes with JavaScript.

3. if… Else statements

The function is to perform conditional judgment.

The flowchart is as follows:

If she likes me, my girlfriend is a goddess. Otherwise, I’m still single.”

3.1 standard if.. else

The recommended writing is as follows:

if (accept === true) {console.log('Liu Yifei is my girlfriend');
}else{
    console.log('I'm such a licking dog.');
}

Copy the code

3.2 the switch.. case

If you have more than one harem, you can use the switch statement to select. The following is an example:

function whichGirlFriend(who){
    switch (who){
        case 'Wang Bingbing':
            console.log('Your girlfriend is Wang Bingbing');
            break;
        case 'Liu Yifei':
            console.log('Your girlfriend is Liu Yifei');
            break;
        case 'Kitty Xue':
            console.log('Your girlfriend is Kitsch.');
            break;
        default:
            console.log('Stop fooling'); }}Copy the code

3.3 Ternary operators

If you’re comfortable, you can also write the ternary operator:

let a = 2;
let b = 3;

let c = 0;

// The following two ways are completely equivalent

// if--else
if (a > b){
    c = a;
}else{
    c = b;
}

// Three-object sentence
c = (a > b) ? a : b;
Copy the code

4. For and while statements

Function: circulation.

For example, while is manual and for is automatic.

Manual gear opening method:

// The usual while statement

let n = 0;
while(n <= 100){
    n++;
    console.log(n);
}

// do... while

do{
    n++;
    console.log(n);
}while(n < 100)
Copy the code

In everyday situations, without using do… For example, Python does not support do at all… While the mechanism.

The above code uses the automatic drive method:

for(var i = 0; i <= 100; ++i){
    console.log(i);
}
Copy the code

Using a for loop is more convenient than using a while loop to traverse an array.

5. Break and continue

These are standard in every programming language.

  • break: is used to force closure and condition determination, usually with an if statement nested within a for or while loop. Equivalent to oneTime bomb.
  • continue: is used to skip the loop, usually with an if statement nested within a for or while loop. It’s like jumping over a mine when you know it’s going to go off.

6. The label statement

Used with break and continue, a tag is a statement preceded by an identifier that can be referenced. Here’s an example from MDN:

let str = ' ';

loop1: // label
for (let i = 0; i < 5; i++) {
  if (i === 1) {
    continue loop1;
  }
  str = str + i;
}

console.log(str);
// output: "0234"
Copy the code

General use, not necessary.

References

  • [1] JavaScript First Steps
  • [2] JavaScript identifier
  • [3] Python3 Conditional Control — a tutorial for beginners
  • [4] JavaScript while statement
  • [5] label statement