aarrow_backPrevious

Data Types

The latest ECMAScript standard defines seven data types:

Although these data types are a relatively few in number, they enable you to perform useful functions within your applications. Objects and functions are the other fundamental elements in the language. Objects may be thought of as named containers for values, and functions as procedures that your application can perform.

if...else Statements

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows:

if (condition) {

statement_1;

} else {

statement_2;

};

(condition) can be any expression that evaluates to true or false. See Boolean for an explanation of what evaluates to true and false.

If condition evaluates to true, statement_1 is executed; otherwise, statement_2 is executed.

statement_1 and statement_2 can be any statement, including further nested if statements.

You may also compound the statements using "else if" to have multiple conditions tested in sequence, as follows:

if (condition_1) {

statement_1;

} else if (condition_2) {

statement_2;

} else if (condition_n) {

statement_n;

} else {

statement_last;

};

In the case of multiple conditions only the first logical condition which evaluates to true will be executed. To execute multiple statements, group them within a block statement ({ ... }) . In general, it's good practice to always use block statements, especially when nesting if statements:

if (condition) {

statement_1_runs_if_condition_is_true;

statement_2_runs_if_condition_is_true;

} else {

statement_3_runs_if_condition_is_false;

statement_4_runs_if_condition_is_false;

};

It is advisable to not use simple assignments in a conditional expression, because the assignment can be confused with equality when glancing over the code. For example, do not use the following code:

if (x = y) {

/* Statements Here */;

};

If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:

if ((x = y)) {

/* Statements Here */;

};

while Statements

A while statement loops through and executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:

while (condition) {

statements;

};

If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.

The condition test occurs before statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again. If the condition returns false, execution stops and control is passed to the statement following while.

To execute multiple statements, use a block statement ({ ... }) to group those statements.

Example:

The following while loop iterates as long as n is less than three:

let n = 0;

let x = 0;

while (n < 3) {

n++;

x += n;

};

With each iteration, the loop increments n and adds that value to x. Therefore, x and n take on the following values:

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.

Function Declarations

A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:

For example, the following code defines a simple function named square:

function square(number) {

return number * number;

};

The function square takes one argument, called number. The function consists of one statement that says to return the argument of the function (that is, number) multiplied by itself. The return statement specifies the value returned by the function.

return number * number;

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

Reference

All documentation present in this basic guide has been provided courtesy of the MDN web docs JavaScript Guide.

Thank you to FreeCodeCamp for the fanstastic curriculum and project inspiration.