Section 2 JavaScript syntax, statements, comments, variables, data types, etc.

1. JavaScript syntax

  • 1. JavaScript literals
  1. Number literal: It can be an integer, a decimal, or scientific notation.

Such as: 3.14, 1001, 123e5

  • String literal: You can use single or double quotes.

For example: "John Baby", 'John Baby'

  • Expression literal: used for calculations

For example: 5+6, 5*10

  • Array literal: defines an array.

For example: [40, 100, 66, 7,20]

  • Object literal: defines an object.

For example: {name: "Zhang Sanfeng", age: 112, sex: "male"}

  • Function literal: defines a function.

For example: function myFunction(a, b){Return a*b;}

2. JavaScript variables

In programming languages, variables are used to store data values.

In JavaScript, the keyword var is used to define variables, and the equal sign (=) is used to assign values ​​to variables.

Example:
     var a;

was age;

a = 12;

age = 22;

3. JavaScript operators

JavaScript uses arithmetic operators to calculate values

JavaScript uses assignment operators to assign values ​​to variables

Example:

x = 5;

y = 6;

z = (x+y)*10;

Assignment, arithmetic and bitwise operators: = + - * /

Conditional, comparison and logical operators: == != < >

2. JavaScript statements

document.getElementById("demo").innerHTML = "Hello Dolly";

The above JavaScript statement outputs the text "Hello Dolly" to the HTML element with id="demo".

1. semicolon;

Semicolons are used to separate JavaScript statements.

Normally we add a semicolon at the end of each executable statement.

Another use for using semicolons is to write multiple statements on one line.

2. JavaScript code blocks

JavaScript can be combined in batches.

A code block starts with an opening curly brace and ends with a closing curly brace.

The purpose of a code block is to execute a sequence of statements together.

3. JavaScript statement indicator

The following table lists JavaScript statement identifiers (keywords):

statement

describe

break

Used to break out of loops.

catch

Statement block, execute the catch statement block when an error occurs during the execution of the try statement block.

continue

Skip an iteration in the loop.

do ... while

Execute a block of statements and continue executing the block if the conditional statement is true.

for

A block of code can be executed a specified number of times when the conditional statement is true.

for ... in

Used to traverse the properties of an array or object (loop through the properties of an array or object).

function

define a function

if ... else

Used to perform different actions based on different conditions.

return

exit function

switch

Used to perform different actions based on different conditions.

throw

Throws (generates) an error.

try

Implement error handling, used with catch.

was

Declare a variable.

while

When the conditional statement is true, the statement block is executed.

4. Space

JavaScript will ignore extra spaces. You can add spaces to your script to make it more readable. The following two lines of code are equivalent:

3. JavaScript comments

1. Single-line comments start with //

2. Multi-line comments start with /* and end with */

  1. Using comments at the end of a line is also //

  • JavaScript variables

1. JavaScript variable naming

  1. Variables must start with a letter
  2. Variables can also start with $ and _ symbols (not recommended)
  3. Variable names are case-sensitive (y and Y are different variables)

Note: JavaScript statements and JavaScript variables are case-sensitive.

2. Declare (create) JavaScript variables

Creating variables in JavaScript is often called "declaring" the variable

We use the var keyword to declare variables:

var carName;

The carName variable is empty (no value) after it is declared like this. If you need to assign a value to the variable, please use the equal sign:

carName = “Jobo”;

In fact, we can do it in one step and assign a value to the variable when declaring it:

var carName = “Jobo”;

3. Redeclare JavaScript variables

var carName = “Jobo”;

var carName;

Note: After the above two statements are executed, the value of the variable carName is still "Jobo". This shows that the value of the variable will not be lost if the JavaScript variable is redeclared.

5. JavaScript data types

Value type (basic type): String, Number, Boolean, Null, Undefined, Symbol.

Reference data type (object type): Object, Array, Function, and two special objects: RegExp and Date.

6. JavaScript functions

1, a function is a code block wrapped in curly braces, with the keyword function used in front:

funcction myFunction(){

//Execute code

}

2. Declaration and call of function pairs with parameters:

statement:

funcction myFunction(var1,var2){

//Execute code

}

transfer:

myFunction(argument1, argument2);

Note:Variables and parameters must appear in the same order. The first variable is the given value of the first parameter passed, and so on.

3. Functions with return values

This can be achieved by using the return statement.

When using the return statement, the function stops execution and returns the specified value.

function myFunction(){

var x = 5;

return x;

}

transfer:

var myValue = myFunction();

The value of the myValue variable is 5, which is the return value of the function "myFunction()".

Guess you like

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