JavaScript statements--expression statements, block statements, empty statements, and declaration statements

Source: Internet
Author: User
Tags cos


Previous words


If an expression is a phrase in JavaScript, then the statement (statement) is a JavaScript whole sentence or command. An expression evaluates a value that the statement uses to execute to make something happen. A JavaScript program is nothing more than a collection of executable statements that the JavaScript interpreter executes sequentially, in accordance with the order in which they were written. This article describes the four types of statements in JavaScript statements--expression statements, block statements, empty statements, and declaration statements





An expression statement


Expression statement is the simplest statement in JavaScript, with assignments, deletes, and function calls, which are expressions and statements, so called expression statements

// Assignment statement
greeting = ‘hello‘ + name;
i * = 3;

// Increment operator (++) and decrement operator (-) are related to assignment statements, their role is to change the value of a variable, just like executing an assignment statement
counter ++;

// delete operator
delete o.x;

// function call
alert (greeting);
window.close ();
cs = Math.cos (x);
The javascript statement ends with a semicolon, but the expression does not need to end with a semicolon. Once you add a semicolon after the expression, the javascript engine will treat the expression as a statement, which will produce some meaningless statements (except expression statements)

1 + 3;
‘Abc’;
 

Block statement
Block statements are also called compound statements. Javascript combines multiple statements together to form a compound statement.

Compound sentences only need to enclose multiple sentences in curly braces

{
    x = Math.PI;
    cx = Math.cos (x);
    console.log (cx);
}
Regarding block statements, there are the following precautions:

【1】 The end of the statement block does not need a semicolon. The original statement in the block must end with a semicolon, but the statement block does not need to

[2] The lines in the statement block are indented, which is not necessary, but neat indentation makes the code more readable and easier to understand

[3] There is no block-level scope in JavaScript, and the variables declared in the statement block are not private to the statement block (excluding es6)

{
    var i = 0;
    console.log (i ++); // 0
}
console.log (i ++); // 1
 

Empty statement
In JavaScript, when you want multiple statements to be used as a single statement, use compound statements instead. An empty statement is just the opposite. It allows 0 statements

;
The javascript interpreter will not perform any action when it executes an empty statement. But when creating a loop with an empty loop body, the empty statement is useful

In the following loop, all operations are completed in the expression a [i ++] = 0, and there is no need for any loop body here. However, JavaScript needs to contain at least one statement in the loop body, therefore, only a single semicolon is used here to represent an empty statement

// Initialize an array a
for (i = 0; i <a.length; a [i ++] = 0);
The semicolon after the right parenthesis of the for, while loop, or if statement is unremarkable, which may cause some bugs, which are difficult to locate

// Because of the excess of ;, it causes different results than expected
if ((a == 0) || (b == 0));
o = null;
If you need to use an empty statement for special purposes, it is best to add comments in the code, so that it can be more clearly explained that this empty statement is useful

for (i = 0; i <a.length; a [i ++] = 0) / * empty * /;
 

Statement
Declaration statement includes variable declaration and function declaration, using var and function keywords respectively

The var statement is used to declare one or more variables. The keyword var is followed by a list of variables to be declared. Each variable in the list can have an initialization expression to specify its initial value.

var name_1 [= value_1] [, ..., name_n [= value_n]]
var i;
var j = 0;
var p, q;
var x = 2, y = r;
Keyword function is used to define the function, funcname is the identifier of the name of the function to be declared, the parameter list is in parentheses after the function name, and the parameters are separated by commas. When calling a function, these identifiers refer to the arguments passed into the function

The function body is composed of javascript statements, the number of statements is not limited, and enclosed in curly braces. When defining a function, the statement in the function body is not executed, it is associated with the new function object to be executed when the function is called

function funcname ([arg1 [, arg2 [..., argn]]]) {statement}
The curly braces in the function statement are required, which is different from the statement block used by while loop and some other statements. Even if the function body contains only one statement, it must still be enclosed in curly braces.

//correct
function hypotenuse (x, y) {
    return Math.sqrt (x * x + y * y);
}
//error
function hypotenuse (x, y)
    return Math.sqrt (x * x + y * y);
The function declaration statement and the function definition expression contain the same function name, but the two are different

//expression
var f = function (x) {return x + 1;}
// Statement
function f (x) {return x + 1;}
Only the variable declaration of the function definition expression is advanced, and the initialization code of the variable is still in its original position; while the function name and function body of the function declaration statement are in advance, all functions in the script and all nested functions in the function will be in the current context Declare before other code, that is, you can call it before declaring a javascript function

console.log (f1 (0)); // Uncaught TypeError: f1 is not a function
var f1 = function (x) {return x + 1;}

console.log (f2 (0)); // 1
function f2 (x) {return x + 1;}
Variable declaration statements and function declaration statements have several similarities

[1] Variable declaration statements and function declaration statements will be advanced

console.log (a); // undefined
var a = 0;
console.log (a); // 0

console.log (f (0)); // 1
function f (x) {return x + 1;}
console.log (f (0)); // 1
[2] Variables created by variable declaration statements and function declaration statements cannot be deleted

var a = 0;
delete a;
console.log (a); // 0

function f (x) {return x + 1;}
delete f;
console.log (f (0)); // 1
 

References
[1] ES5 / statement https://www.w3.org/html/ig/zh/wiki/ES5/statements
[2] Ruan Yifeng Javascript Standard Reference Course-Grammar Overview http://javascript.ruanyifeng.com/grammar/basic.html#toc12
[3] Chapter 5 of the "Authoritative Guide to JavaScript (6th Edition)"

javascript statement-expression statement, block statement, empty statement and statement statement

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.