JavaScript Basic Grammar _ Basics

Source: Internet
Author: User
Tags arithmetic arithmetic operators arrays logical operators

1, operator

An operator is a sequence of symbols that completes an operation, and it has seven classes:

Assignment operators (=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=), arithmetic operators (+,-, *,/,++,--,%), Comparison operators (>,<,<=,>=,==,== =,!=,!==), logical operators (| |,&&,!), conditional operations (?:), displacement operators (|,&,<<,>>,~,^), and string operators (+).

Many people may not know what "= = =" is.

Here, I explain to you that in JavaScript, "= =" is the equivalent operator.

Comparison rules for equivalent operators:

When the type of two operands is not the same: convert them to the same type

1 A number is compared with a string, after the string is converted to a number.
2 true converts to 1, false to 0, for comparison.
3 an object, array, function, and a number or string, objects, arrays, functions are converted to values of the original type, and then compared. (Use valueof First, if not, use ToString)
4 other types of combinations are not stars.

You want to have the same type of two operands, or after converting to the same type:

1) 2 strings: The characters at the same position are equal and 2 strings are the same.
2) 2 digits: 2 numbers are the same, the same. If one is Nan, or two are Nan, it is not the same.
3) 2 are true, or 2 are false, the same.
4) 2 refer to the same object, function, array, they are equal, if the reference is not the same object, function, array, it is not the same, even if the 2 objects, functions, arrays can be converted to the original value of exactly equal.
5) 2 null, or 2 are undefined, then they are equal.

and "= = =" are all identical operators, the same operator follows the comparison rule of the equivalent operator, but it does not type the operand, returns False when the type of the two operands is not the same, and only follows the comparison rule of the equivalent operator when the type of the two operands is the same.

For example: Null==undefined will return true, but null===undefined will return false!

2. Expression type

The combination of operators and operands, called expressions, is typically divided into four classes: assignment expressions, arithmetic expressions, Boolean expressions, and string expressions.

3, statements

A JavaScript program consists of several statements, which are instructions for writing a program. JavaScript provides a complete set of BASIC programming statements, which are:
Assignment statements, switch selection statements, while loop statements, for loop statements, for Each loop statements, do-while loop statements, break-loop abort statements, continue loop interrupt statements, with statements, try...catch statements,
If statement (if.. Else,if...else If ... ), let statement.

4. function

A function is a named segment of a sentence that can be referenced and executed as a whole. Use functions to note the following points:

1 The function is defined by the keyword function (or by the function constructor).
2 functions defined using the Function keyword are within a scope that can be invoked at any place (including before the statement that defines the function), and the definition of the VAR keyword must be defined before it can be invoked.
3 function name is the name that is referenced when calling the function, it is sensitive to case, and cannot write wrong function name when calling function.
4 The parameter represents the value passed to the function's use or operation, which can be a constant, or a variable or a function, which can be accessed by the arguments object (the arguments object is a pseudo array, and the property callee refers to the called function).
5 The Return statement returns the value of an expression.
6 The yield statement throws an expression, and the interrupt function executes until the next call to next.

General functions are in the following format:

Copy Code code as follows:
function MyFunction (params) {
Executed statement
}

function expression:

Copy Code code as follows:
var myfunction=function (params) {
Executed statement
}

Copy Code code as follows:
var myfunction = function () {
Executed statement
}

Copy Code code as follows:
MyFunction ()//Call function

An anonymous function that is often passed as a parameter between other functions:

Copy Code code as follows:
Window.addeventlistener (' Load ', function () {
Executed statement
},false);

5. Object

An important function of JavaScript is the object-oriented function, which can be developed in a more intuitive, modular and reusable way through object-based programming.

A set of properties that contain data and a method for manipulating data in a property, called an object. For example, to set the background color of the Web page, the object is the document, the property name used is bgcolor, such as document.bgcolor= "Blue", that is to make the background color blue.

6. Events

An action that occurs when a user interacts with a Web page, called an event. Events can be triggered by the user, or changes to the page, and even events that you can't see (such as the interactive progress of Ajax). Most of the events are triggered by the user's actions, such as: the user presses the mouse button, produced click event, if the mouse pointer on the link to move, it generates mouseover events and so on. In JavaScript, events are often used in support of event handlers.

As for handling events, the method of the AddEventListener () is to use the () function, which has three parameters: event, the function raised, whether to use event capture. For security, it is recommended to always set the third parameter to false;

The traditional approach is to define the element's on ... event, which is the event parameter in the methods of the method of the consortium before adding an "on". The event model of IE uses attachevent and dettachevent to bind and delete events. Events in JavaScript are also captured and bubbling in two stages, but traditional bindings only support bubbling events.

7, variable

such as var myvariable = "some value";

The variable has its type, and the myvariable type is string (string) in the example above

Common types of JavaScript support are:

Object: Objects
Array: Arrays
Number: Numbers
Boolean: Boolean value, only true and false two values, is the least memory in all types
NULL: A null value with a unique value of NULL
Undefined: No variables defined and assigned

Actually the JavaScript variable is a weak variable type, and you assign it to him as a string.
It's numbers, he's plastic. Is true and false he is a Boolean (note that you cannot add quotes, or you will be treated as a string).

The above is JS Basic grammar, I hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.