Section 3 JavaScript functions, scopes, events, strings, operators, comparisons

1. Scope of JavaScript

1. The variable is declared within the function. The variable is a local variable and has a local scope.

Local variables: can only be accessed inside the function

Example:

// The carName variable cannot be called here

function myFunction() {

    var carName = "Volvo";

    //The carName variable can be called within the function

}

Note: Because local variables only act within a function, the same name can be used as a variable name in different functions.

 Local variables are created when the function starts executing, and are automatically destroyed after the function is executed.

2. Variables defined outside the function are global variables.

Example:

var carName = " Volvo";

// The carName variable can be called here

function myFunction() {

    //The carName variable can be called within the function

}

Note: Global variables have global scope and can be used by all scripts and functions in the web page.

3. JavaScript variable life cycle

The lifetime of a JavaScript variable is initialized when it is declared.

Local variables are destroyed after the function completes execution.

Global variables are destroyed after the page is closed.

Note:
The parameters of the function only work within the function, so it is a local variable.

3. JavaScript events

Common HTMML event tables:

event

describe

onChange

HTML element changes

onClick

User clicks on HTML element

onmouseover

Occurs when the mouse pointer moves over the specified element

onmouseout

Occurs when the mouse pointer moves away from the specified element

onKeyDown

User presses keyboard key

onLoad

The browser has finished loading the page

String - escape characters:

4. JavaScript operators

1. Arithmetic operators

Arithmetic operations between AND/OR values.

y=5, the following table explains these arithmetic operators:

operator

describe

example

x operation result

y operation result

+

addition

x=y+2

7

5

-

Subtraction

x=y-2

3

5

*

multiplication

x=y*2

10

5

/

division

x=y/2

2.5

5

%

remainder

x=y%2

1

5

++

self-increasing

x=++y

6

6

x=y++

5

6

--

Decrease

x=--y

4

4

x=y--

5

4

2. Assignment operator

The assignment operator is used to assign values ​​to JavaScript variables.

Given x=10 and y=5, explain the assignment operator:

operator

example

Equivalent to

Operation result

=

x=y

x=5

+=

x+=y

x=x+y

x=15

-=

x-=y

x=x-y

x=5

*=

x*=y

x=x*y

x=50

/=

x/=y

x=x/y

x=2

%=

x%=y

x=x%y

x=0

3, + operator for strings

The + operator is used to concatenate text values ​​or string variables.

If you concatenate two or more string variables, use the + operator

For example:

text01 = “Hello”;

text02 = “ World”;

test03 = text01+ text02;

Operation result: Hello World

4, add strings and numbers

Adds two numbers and returns the sum of the added numbers. If a number is added to a string, a string is returned:

Example 1:

age1 = 12;

age2 = 8;

age3 = age1 + age2;

Operation result:

age3 = 20;

Example 2:

age1 = 12;

text2= “23”;

age3 = age1 + text2;

Operation result:

age3 = 1223; (string)

5, comparison sign

Comparison operators are used in logical statements to determine whether variables or values ​​are equal.

Given x=5, the following table explains the comparison operators:

operator

describe

Compare

return value

==

equal

x==8

false

==

equal

x==5

true

===

Absolute equality (both value and type are equal)

x===“8”

false

===

Absolute equality (both value and type are equal)

x===5

true

!=

not equal to

x!=“8”

true

!==

Not absolutely equal (one or both of the value and the type are not equal)

x!=="5"

true

!==

Not absolutely equal (one or both of the value and the type are not equal)

x!==5

false

>

more than the

x>8

false

<

less than

x<8

true

>=

greater than or equal to

x>=8

false

<=

less than or equal to

x<=8

true

How is it generally used?

You can compare operation values ​​in conditional statements to obtain different results:

like:

If(age < 18) {

x = “Too Young”;

}

6、迻辑运计

Logical operators are used to determine the logic between variables or values.

Given x=8 and y=4, the following table explains the logical operators:

operator

describe

example

&&

and

( x & lt; 10 && y > 1) One true

||

or

(x==5 || y==5) is false

not

!(x==y) is true

illustrate:

&&: The result is true only when the conditions on the left and right sides of the operator are true.

||: As long as one of the conditions on the left and right sides of the operator is true, the result will be true. On the contrary, the result is false only when both sides of the operator are false.

!: Negation, when x==y is true,! (x==y) is false.

7, conditional operator

JavaScript also includes conditional operators that assign values ​​to variables based on certain conditions.

grammar

variablename=(condition)?value1:value2 

That is: when condition is true, variablename = value1;

      When condition is false, variablename = value2;

If the value in the variable age is less than 18, assign the value "age is too young" to the variable voteable, otherwise assign the value "age has reached".

voteable=(age<18)?"Too young":"Age reached";

Guess you like

Origin blog.csdn.net/yyxhzdm/article/details/134902357