JavaScript syntax

JavaScript Syntax

JavaScript is a scripting language.

It is a lightweight, yet powerful programming language.

Operator

The operator is a series of symbols that completes the operation. It has seven categories:

Assignment operator (=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=),

Arithmetic operators (+,- ,*,/,++,--,%),

Comparison operators (>,<,<=,>=,==,===,!=,!= =),

Logical operators (||,&&,!),

Conditional operations (?:),

Shift operators (|,&,< <,>>,~,^) and the string operator (+).

Expression

The combination of operators and operands is called an expression, which is usually divided into four categories: assignment expressions and arithmetic expressions expressions, Boolean expressions, and string expressions.

Statements

Javascript programs are composed of several statements, and statements are instructions for writing programs. Javascript provides a complete set of basic programming statements, which are:
Assignment statement, switch selection statement, while loop statement, for loop statement, for each loop statement, do while loop statement, break loop interruption statement, continue loop interruption statement, with statement, try...catch statement,
if statement (if..else, if...else if...), let statement.

Function

A function is a named statement segment that can be referenced and executed as a whole. Pay attention to the following points when using functions:

1) Functions are defined by the keyword function (can also be constructed by the Function constructor).
2) A function defined using the function keyword can be called anywhere within a scope (including before the statement that defines the function); while a function defined using the var keyword must be defined before it can be called.
3) The function name is the name quoted when calling the function. It is case-sensitive. You cannot write the wrong function name when calling the function.
4) The parameter represents the value passed to the function for use or operation. It can be a constant, a variable, or a function. Inside the function, the arguments object can be passed (the arguments object is a pseudo array, and the attribute callee is referenced by function called) has access to all parameters.
5) The return statement is used to return the value of the expression.
6) The yield statement throws an expression and interrupts function execution until the next call to next.

General functions have the following format:

var myFunction=function(params){
//Executed statement
}

var myFunction = function(){
//Executed statement
}

Keywords

JavaScript statements usually begin with keywords. The var keyword tells the browser to create a new variable:

var x = 5 + 6;
var y = x * 10;


Keywords

#Like any other programming language, JavaScript reserves some keywords for its own use.

JavaScript also reserves some keywords that are not used in the current language version, but will be used in future JavaScript extensions.

JavaScript keywords must begin with a letter, an underscore (_), or a dollar sign ($).

Following characters can be letters, numbers, underscores or dollar signs (numbers are not allowed to appear as the first character, so that JavaScript can easily distinguish keywords and numbers).


#Object

An important function of Javascript is the object-oriented function. Through object-based programming, program development can be carried out in a more intuitive, modular and reusable way.


A set of properties containing data and methods for operating on the data contained in the properties is called an object. For example, if you want to set the background color of a web page, the target object is document, and the attribute name used is bgcolor. For example, document.bgcolor="blue" means that the background color is blue.


event

The operations that occur when a user interacts with a web page are called events. Events can be triggered by users, page changes, or even events you can't see (such as Ajax interaction progress changes). Most events are triggered by user actions. For example, when the user presses a mouse button, a click event is generated. If the mouse pointer moves on a link, a mouseover event is generated, etc. In Javascript, events are often used in conjunction with event handlers.

For event processing, the W3C method is to use the addEventListener() function, which has three parameters: the event, the function that is triggered, and whether to use event capture. For the sake of security, it is recommended to always set the third parameter to false;

The traditional method is to define the on... event of the element, which is the W3C method with "on" added before the event parameter. IE's event model uses attachEvent and detachEvent to bind and delete events. Events in JavaScript are divided into two stages: capturing and bubbling, but traditional binding only supports bubbling events.


Variable

##such as var myVariable = "some value";

A variable has its type. In the above example, the type of myVariable is string (string)

The common types supported by javascript are:

object : Object

array: Array
number: Number
boolean: Boolean value, with only two values: true and false, which takes up the least memory among all types
null: a null value, the only value Is null
undefined: a variable that has no definition or assignment

In fact, JavaScript variables are weak variable types. What you assign to them is a string, and it is a String.

If it is a number, it is an integer. . If it is true or false, it is of boolean type (note that quotation marks cannot be added, otherwise it will be treated as a string).


##Case sensitive

JavaScript is case sensitive.

When writing JavaScript statements, please pay attention to whether the case switching key is turned off.

The functions getElementById and getElementbyID are different.

Similarly, variables myVariable and MyVariable are also different.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var my_name = "Jack"; var age = 25; alert("我的名字叫" + my_name + ",我今年" + age + "岁。"); </script> </head> <body> <p></p> </body> </html>
submitReset Code