An array of 1 –

1.1 The concept of arrays

  • Arrays hold groups of related data together and provide easy access.
  • An array is a collection of data, each of which is called an element. An array can hold any type of element. Arrays are an elegant way to store a set of data under the name of a single variable.

1.2 Creating an Array

There are two ways to create arrays in JS:

  • Create an array using new

    varArray name =new Array(a);var arr = new Array(a);// Create a new empty array
    Copy the code

    Notice Array (), capital A

  • Create an array using an array literal

    //1. Create an empty array using array literals
    varArray name = [];//2. Use array literals to create arrays with initial values
    varArray name = ['white'.'black'.'rhubarb'.'rich'];
    Copy the code
    • Array literals are square brackets []
    • Declaring an array and assigning a value to it is called array initialization
    • This is the literal approach that we’re going to use the most in the future
  • The type of an array element

    Arrays can hold any type of data, such as strings, numbers, booleans, etc.

    var arrStus = ['white'.12.true.28.9];
    Copy the code

1.3 Obtaining an element in an array

Index (subscript) : The ordinal number used to access an array element (array subscripts start at 0).

An array can be accessed, set, or modified by an index, or retrieved by an “array name [index]”.

// Define an array
var arrStus = [1.2.3];
// Get the second element in the array
alert(arrStus[1]);    
Copy the code

Note: If the array is accessed without an element corresponding to the index value, the value is undefined

1.4 Go through the number group

  • Array traversal

    Access each element in the array once from beginning to end (similar to a student roll call) and iterate through each item in the array through the for loop index

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

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

    Array name.length is used to access the number of elements in the array (array length).

    var arrStus = [1.2.3];
    alert(arrStus.length);  / / 3
    Copy the code

    Note:

    • The length of the array is the number of elements in the array, not to be confused with the array index.
  • When the number of elements in our array changes, the length property changes with it

    • The length property of the array can be modified:
  • If the length attribute is greater than the number of elements in the array, a blank element appears at the end of the array.

    • If the length attribute is set to a value less than the number of elements in the array, the array elements exceeding that value are removed

1.5 New element in array

New elements can be inserted at the end of arrays in the following ways:

Array [array.length] = new data;Copy the code

2 – function

2.1 Concept of functions

In JS, there may be a lot of code that is identical or functionally similar, and this code may need to be reused a lot. While the for loop can do some simple repetitive operations, it is more limited, so we can use functions in JS instead.

Function: encapsulates a block of code that can be repeatedly called and executed. You can reuse a lot of code with this block.

2.2 Use of functions

Declare functions

// Declare the function
functionThe function name () {
    // Function body code
}
Copy the code
  • Function is the keyword for declaring functions and must be lowercase

  • Since functions are defined to do something, we usually name them as verbs, such as getSum

Call a function

// Call the functionThe function name ();Execute the function body code by calling the function name
Copy the code
  • Don’t forget to add parentheses when calling

  • Tip: a function is not called and does not execute itself

    Note: declaring the function itself does not execute the code; only the function body code is executed when the function is called.

Function encapsulation

  • Function encapsulation encapsulates one or more functions in the form of functions and provides only a simple function interface

  • Simple understanding: Packaging is similar to integrating computer components into a case (similar to express packaging)

Example: Encapsulate computes the 1-100 summation

/* A function that computes values between 1 and 100 */
// Declare the function
function getSum(){
  var sumNum = 0;// Prepare a variable, save the numbers and
  for (var i = 1; i <= 100; i++) {
    sumNum += i;// Add each value to the variable
  }
  alert(sumNum);
}
// Call the function
getSum();
Copy the code

2.3 Parameters of the function

Function argument syntax

  • Parameter: when a function is defined, it is passed in when the call is received

  • Argument: The real data passed in parentheses when the function is called

What arguments do: Some values cannot be fixed inside a function. We can use arguments to pass in different values when calling a function.

Application of function parameters:

// Function declaration with arguments
functionThe function name (parameter1The parameter2The parameter3..) { // You can define as many arguments as you want, separated by commas
  / / the function body
}
// Function call with argumentsFunction name (argument1That argument2That argument3...) ;Copy the code
  1. The argument value is passed to the parameter when called
  2. Parameters are simply undeclared variables
  3. Arguments and parameters are separated by commas (,)

When the number of parameters and arguments of a function do not match

Note: In JavaScript, the default value of a parameter is undefined.Copy the code

Summary:

  • A function can take arguments or no arguments
  • When declaring a function, the parameters inside the parentheses are parameters that default to undefined
  • When you call a function, the arguments inside the parentheses are arguments
  • Multiple parameters are separated by commas
  • The number of parameters may not match the number of arguments, but the result is unpredictable, so we try to match

2.4 Return value of the function

Return statement

Return value: the data represented by the function call as a whole; After the function is executed, the specified data can be returned using the return statement.Copy the code
// Declare the function
functionFunction name (){...returnThe value to return; }// Call the functionThe function name ();// Call the function to get the value after return
Copy the code
  • When a return statement is used, the function stops execution and returns the specified value
  • If the function does not return, undefined is returned

Break,continue,return

  • Break: to end the body of the current loop (for, while)
  • Continue: To break out of the current loop and continue with the next loop (for, while)
  • Return: Not only can you exit the loop, you can also return the value of the return statement, and you can also end the code inside the current function

2.5 Arguments

You can use arguments to get arguments when you are unsure how many arguments to pass. In JavaScript, arguments is actually a built-in object for the current function. All functions come with a built-in arguments object, which stores all arguments passed. Arguments presentation form is a pseudo-array, so it can be iterated over. Pseudo-arrays have the following characteristics:

  • Has the length property

  • Store data by index

  • Methods push, pop, etc. without arrays

    Note: This object is used inside a function to get the arguments passed when the function is called.

2.6 Function Cases

A function can be called from within another function. In the same scoped code, the function name represents the encapsulated operation, which can be executed by enclosing the function name in parentheses.Copy the code

2.7 Two ways to declare functions

  • Custom functions (named functions)

    Custom function methods using the function keyword function

    // Declare the definition mode
    function fn() {... }/ / call
    fn();  
    Copy the code
    • They are also called named functions because they have names
    • The code that calls a function can be placed either before or after the declared function
  • Function expression mode (anonymous function)

    The function expression is written as follows:

    // This is a function expression. Anonymous functions are terminated by semicolons
    var fn = function(){... };The function call must be written below the function body
    fn();
    Copy the code
    • Because functions have no names, they are also called anonymous functions
    • So this fn is storing a function
    • The principle of functional expression is the same as that of declaring variables
    • The code for a function call must be written after the function body