JS basic knowledge (array, function)

Array

concept

  • Array can store a group of related data together and provide convenient access (acquisition)
  • An array refers to a collection of data, each of which is called an element, and any type of element can be stored in the array
  • Array is an elegant way to store a set of data under a single variable name

Create array

Use new to create an array

var 数组名 = new Array() ;

Create an array using array literals

var  数组名 = [];

The literal of the array is square brackets [] to
declare the array and assign the value is called the initialization of the array

Get the elements in the array

Index (subscript): the serial number used to access the array element (array subscript starts from 0). The
Insert picture description here
array can be accessed, set and modified by index, and the array can be obtained in the form of "array name [index]" The element
syntax in
var arr = [1,2,3];// Define the array
alert(arr[1]); // Get the second element in the array.
Note that
if the array does not have an element corresponding to the index value when accessed, The value obtained is undefined

Iterate over the array

Array traversal

Each element in the array is visited once from beginning to end (similar to a student's roll call), and each item in the array can be traversed through the for loop index

grammar

var cc = ['red','black', 'yellow'];
for(var i = 0; i < arr.length; i++){
   console.log(cc[i]);
}

The length of the array

  • The length of the array: By default, the number of elements in the array
  • Use "array name.length" to access the number of array elements (array length)
var cc = [1,2,3];
alert(cc.length);

New element in the array

grammar

数组[ 数组.length ] = 新数据;

function

Concept
Function is to encapsulate a block of code that can be repeatedly called and executed. Through this code block, a large amount of code can be reused.

Use of functions

Declare function

function 函数名() {
  //函数体代码
}

function is the keyword for declaring functions and must be lowercase

Call functions

函数名();

Declare that the function itself will not execute the code, the function body code will only be executed when the function is called

Function encapsulation

Function encapsulation is to encapsulate one or more functions through functions, and only provide a simple function interface to the outside

Function parameters

Function parameter syntax

Insert picture description here
Function:
Some values ​​cannot be fixed inside the function, we can pass different values ​​in when calling the function through parameters

When the function parameters and actual parameters do not match

Insert picture description here

The return value of the function

return statement

The data represented by the function call as a whole; after the function is executed, the specified data can be returned to the syntax through the return statement

function 函数名(){
  ...
  return  需要返回的值;
}
函数名(); 

The difference between break, continue, return

  • break: end the current loop body (such as for, while)
  • continue: Jump out of this loop and continue to execute the next loop (such as for, while)
  • return: Not only can you exit the loop, you can also return the value in the return statement, and you can also end the code in the current function body

Two ways of function declaration

Custom function method (named function)

Use the function keyword function to customize the function method

grammar

function fn() {...}
fn();

The code that calls the function can either be placed before or after the declared function

Function expression mode (anonymous function)

grammar

var fn = function(){...};
fn();

Function call code must be written after the function body

Guess you like

Origin blog.csdn.net/chuenst/article/details/108723370