Statements

Statements

 

are JavaScript commands

 

are terminated with semicolons

 

are executed to make something happen

 

JS programs are nothing more than a sequence of  statements to execute

 

the JS interpreter, by default, executes these statements one after another in the order they are written

Statements

expression statements

expressions with side effects can stand alone as statements (like assignments / function invocations)

declaration statements

which declare new variables and define new functions

control structures

alter the default order of execution:

conditionals

make the interpreter execute or skip other statements depending on the value of an expression (like if/switch)

loops

execute other statements repetitively (like while / for)

jumps

cause the interpreter to jump to another part of the program ( like break / return / throw )

Expression Statements

Expression statements

 

simplest kinds of statements in JS

are expressions that have side effects

 

assignment statements

increment and decrement operators

the delete operator

function calls

 

 ! if a function doesn't have any side effects, there's no sense in calling it,

unless it's part of a larger expression or an assignment statement

Compound statements

a statement block

 

is a sequence of statements enclosed within curly braces { }

 

combines multiple statements into a single compound statement

 

acts as a single statement, thus allows the usage of multiple statements where syntax expects a single statement

 

the block does not end with a semicolon, the primitive statements within the block end with semicolons

 

! indentation, optional but makes the code easier to read and understand

Empty statement

is the opposite of a compound statement

as it allows you to include no statements where one is expected

 

looks like this ";"
 

the interpreter takes no action when it executes an empty statement

 

the empty statement is occasionally useful when you want to create a loop that has an empty body

 

! when intentionally using the empty statement, it is a good idea to comment code in a way that makes it clear that it has been done on purpose

Declaration Statements

Declaration statements

var and function

 

declare or define identifiers (variables and function names)

that can be used elswhere in the program to be assigned values

 

this type of statements don’t do much themselves,

but by creating variables and functions they, in an important sense, define the meaning of the other statements in your program

var statement

 declares one or more variables

 

 the var keyword is followed by a comma-separated list of variables

 

each variable in the list may optionally have an initializer expression that specifies its initial value

 

when statement appears within the body of a function, it defines local variables, scoped to that function

 

when var is used in top-level code, it declares global variables (properties of the global object, which can't be deleted)
 

var statement

if no initializer is specified, the variable's initial value is undefined

 

variables are defined throughout the script or function

in which they are declared, their declaration is “hoisted” up to the start of the script or function

 

initialization, however, occurs at the location of the var statement, and the value of the variable is undefined before that point in the code

 

var can also appear as part of the for and for/in loops (these variables are hoisted, just like variables declared outside of a loop)

 

! it is harmless to declare the same variable multiple times

function statement

the function keyword is used to define functions,

it can be used as part of an expression and it can also be use in statement form (exemple)

function funcname([arg1 [, arg2 [..., argn]]]) {statements}

funcname is an identifier that names the function being declared

followed by a comma-separated list of parameter names in parentheses - can be used within the body of the function to refer to the argument values passed when the function is invoked

 

the body of the function is composed of any number of statements, contained within curly braces (always required)

the statements are not executed when the function is defined instead, they are associated with the new function object for execution when the function is invoked

function statement

may appear in top-level code or may be nested within other  functions (but  only  at the top level of the function they are nested within) => function definitions may not appear within if statements, while loops, or any other statements

 

statements differ from expressions in that they include a function name

both forms create a new function object, but the function declaration statement also declares the function name as a variable and assigns the function object to it

 

like variables, function definition statements are implicitly “hoisted” to the top of the containing script or function, so that they are visible throughout the script or function (both function name and body)  =>  a JS function can be invoked before it is declared

 

function declaration statements create variables that cannot be deleted, however these variables are not read-only,  and their value can be overwritten

Control structures

Conditional

statements

Conditional statemets

 

execute or skip other statements depending on the value of a
specified expression

 

are the decision points of your code, and they are also sometimes known as “branches”

if statement

is the fundamental control statement that allows JavaScript to make
decisions, to execute statements conditionally


Statements

By Azaleea Cristina Constantinescu