Learning experience about Javascript

catalogue

preface

Javascript process control

Sequential structure

Branching structure

if statement

  if else statement

  if else if statement

switch Statements

Ternary expression

Cyclic structure

for loop

Double for loop

  Example analysis

while Loop

do...while loop

Termination program in loop structure -- continue, break

Introduction to Javascript arrays

Create array

Create an array with new (not commonly used)

Creating arrays with array literals

Get (access) array

Traversal array

Array traversal

New element in array

preface

For the learning process, every day's state is spent in "100000 whys" 🤯 Every day is another brainstorming ~ but learning is a process from knowing nothing to slowly groping, understanding, and finally mastering and flexible application~

Next, I will briefly sort out the following learning contents of Javascript from my own learning methods and learning framework, including Javascript process control and array.

 

Javascript process control

Sequential structure

Sequential structure is the simplest and most basic process control in a program. It has no specific syntax structure. The program will execute in sequence according to the sequence of codes. Most codes in the program are executed in this way.

 

Branching structure

if statement

Statement can be understood as a behavior. Loop statement and branch statement are typical statements. A program consists of many statements. Generally, it will be divided into statements one by one.

Syntax structure:

// If the condition is true, execute the code, otherwise do nothing
if (Conditional expression) {
    // Code statement executed when the condition is satisfied
}

  if else statement

Syntax structure:

// If the condition is true, execute the code in if, otherwise execute the code in else
if (Conditional expression) {
    // [if] the condition is true, the code to be executed
} else {
    // [otherwise] executed code
}

  if else if statement

Syntax structure:

// Suitable for checking multiple conditions.
if (Conditional expression 1) {
    Statement 1;
} else if (Conditional expression 2)  {
    Statement 2;
} else if (Conditional expression 3)  {
   Statement 3;
 ....
} else {
    // None of the above conditions holds. Execute the code here
}

 

switch Statements

The switch statement is also a multi branch statement, which is used to execute different code based on different conditions. When you want to set a series of options for a specific value for a variable, you can use switch.

Syntax structure:

switch( expression ){ 
    case value1:
        // Code to execute when expression equals value1
        break;
    case value2:
        // Code to execute when expression equals value2
        break;
    default:
        // Code to execute when the expression is not equal to any value
}

be careful:

  • The keyword switch can be followed by an expression or value in parentheses, usually a variable

  • Keyword case, followed by an expression or value of an option, followed by a colon

  • The value of the switch expression is compared with the value of the case in the structure

  • If there is matching congruence (= = =), the code block associated with the case will be executed and stopped when a break is encountered, and the code execution of the whole switch statement will end

  • If the values of all case s do not match the values of the expression, execute the code in default

  •   When executing the statement in the case, if there is no break, continue to execute the statement in the next case.

Ternary expression

Syntax structure:

Expression 1 ? Expression 2 : Expression 3;

Implementation ideas

  • If expression 1 is true, the value of expression 2 is returned; if expression 1 is false, the value of expression 3 is returned

  • Simple understanding: it is similar to the abbreviation of if else

Code understanding:

var num = 0;
        var result = num > 5 ? 'Yes Yes' : 'Da baa!'
        console.log(result);

 

Cyclic structure

for loop

The for loop repeats the same code

Syntax structure:

//  Basic writing
for(var i = 1; i <= 10; i++){
    console.log('Wife, I'm wrong~');
}
// User input times
var num = prompt('Please enter the number of times:');
for ( var i = 1 ; i <= num; i++) {
    console.log('Wife, I'm wrong~');
} 

The for loop repeats different code

Syntax structure:

// Other statements can be added to for 
for (var i = 1; i <= 100; i++) {
 if (i == 1) {
    console.log('This man is one year old. He was born');
 } else if (i == 100) {
    console.log('This man is 100 years old. He's dead');
  } else {
       console.log('This man this year' + i + 'Years old');
  }
}
//That is, when you re output, you can change it with variable i

Double for loop

Syntax structure:

for (Initial of external circulation; Conditions of external circulation; Operation expression of outer loop) {
    for (Initial of internal circulation; Conditions of internal circulation; Inner loop operation expression) {  
       Code to execute;
   }
}

be careful:

  • The inner loop can be regarded as the loop body statement of the outer loop

  • The execution order of the inner loop should also follow the execution order of the for loop

  • The outer loop is executed once, and the inner loop is executed all times

  Example analysis

Example 1: print out the positive triangle stars

var str='';
for(var i=1;i<=10;i++){
   for(var j=1;j<i;j++){
     str + ='⭐';
   }
     str + ='\n'; 
}
console.log(str);

Example 2: print out the 99 multiplication table

var str = '';
        for (var i = 1; i <= 9; i++) {

            for (var j = 1; j <= i; j++) { //The number of columns is less than or equal to the number of rows
                // str += ' ⭐'; Append string
                str += j + 'X' + i + '=' + i * j; //Variables and strings are connected by +
                str += j + 'x' + i + '=' + i * j + '\t';
            }
            str = str + '\n';
        }
        console.log(str);

Example 3: countdown effect "zero filling"

 var time = prompt('Please enter a 0~59 A number between?');
        var result = time < 10 ? '0' + time : time;
        alert(result);

while Loop

Syntax structure:

while (Conditional expression) {
    // Loop body code 
}

be careful:

When using a while loop, you must pay attention to that it must have exit conditions, otherwise it will become an endless loop.

do...while loop

Syntax structure:

do {
    // Loop body code - repeats the loop body code when the conditional expression is true
} while(Conditional expression);

be careful:

Execute the loop body first, and then judge that the do... while loop statement will execute the loop body code at least once.

Termination program in loop structure -- continue, break

The continue keyword is used to immediately jump out of this cycle and continue the next cycle (the code after continue in the body of this cycle will be executed less than once);

The break keyword is used to immediately jump out of the entire loop (the end of the loop).

Introduction to Javascript arrays

Simply put, an array is a collection of data, in which each data is called an element, and then any type of element can be stored in the array.

The stored array elements can store any type of data, such as string, number, Boolean value, etc.

give an example:

var arrStus = ['Xiaobai',12,true,28.9];

Create array

Create an array with new (not commonly used)

Syntax structure:

var Array name = new Array() ;
var arr = new Array();   // Create a new empty array

Note: Array(), A should be capitalized

Creating arrays with array literals

Syntax structure:

//1. Create an empty array using array literal
var  Array name = [];
//2. Create an array with initial value using array literal
var  Array name = ['Xiaobai','Xiao Hei','chinese rhubarb','Ritchey '];

Get (access) array

Definition of index (subscript): the sequence number used to access array elements (array subscript starts from 0).

  Function of index (subscript): the array can access, set and modify the corresponding array elements through the index. The elements in the array can be obtained in the form of "array name [index]".

Syntax structure:

// Define array
var arrStus = [1,2,3];
// Gets the second element in the array
alert(arrStus[1]);    

Traversal array

Array traversal

Definition: each element in the array is accessed from beginning to end (similar to the student's roll call), and each item in the array can be traversed through the for circular index.

Syntax structure:

var arr = ['red','green', 'blue'];
for(var i = 0; i < arr.length; i++){
    console.log(arrStus[i]);
}

Length of array

Array length: indicates the number of elements in the array by default

Use array name. Length to access the number of array elements (array length).

Syntax structure:

var arrStus = [1,2,3];
alert(arrStus.length);  // 3

be careful:

  • Here, the length of the array is the number of array elements. Do not confuse it with the index number of the array.
  • When the number of elements in our array changes, the length attribute changes with it

  • The length property of the array can be modified:

  • If the set length attribute value is greater than the number of elements in the array, a blank element will appear at the end of the array;

  • If the set length attribute value is less than the number of elements of the array, the array elements exceeding this value will be deleted

New element in array

  • Increase the length of the array

Code display:

 var arr = ['red', 'green', 'blue'];
        arr.length = 4; //Directly change the length of the array to 5, and there will be 5 elements in it
        console.log(arr);
        console.log(arr[4]); //undefined
        console.log(arr[5]); //undefined
  • Directly modify the index number and append array elements

Code display:

 var arr1 = ['red', 'green', 'blue'];
        arr1[3] = 'yellow'; //Add yellow after the array
        console.log(arr1);
        arr1[2] = 'pink';
        console.log(arr1); //Changed arr[2]=blue to pin
        arr1 = 'Change the whole array to this name, and there are no elements in it';
        //Do not assign a value to the array name directly, otherwise there will be no array elements in it
        console.log(arr1);

Ending~

This is a simple combing. I will see more results when I study deeply later~

 

Keywords: Javascript Front-end

Added by miked2 on Wed, 17 Nov 2021 03:57:16 +0200