JavaScript statement

Understand the basics of JavaScript statements

If the expression is a single unit of JavaScript that the engine can evaluate, the statement can contain one or more different expressions and be executed by the engine to perform the operation.

The program consists of multiple statements. Statements can span multiple lines.

Just like expressions, JavaScript has a completely different set of statements:

  • Express statement
  • Statement
  • Control flow statement
  • loop statement
  • Miscellaneous statement

Let's delve into the details.

Delimited statement

The statement can end with an optional semicolon;. Using it, you can include multiple statements on one line. I usually don't use a semicolon, but you can use it at the end of the statement.

Express statement

The expression itself is also a statement:

2
0.02
'something'
true
false
this //the current scope
undefined
i //where i is a variable or a constant
1 / 2
i++
i -= 2
i * 2
'A ' + 'string'
[] //array literal
{} //object literal
[1,2,3]
{a: 1, b: 2}
{a: {b: 1}}
a && b
a || b
!a
object.property //reference a property (or method) of an object
object[property]
object['property']
new object()
new a(1)
new MyRectangle('name', 2, {a: 4})
function() {}
function(a, b) { return a * b }
(a, b) => a * b
a => a * 2
() => { return 2 }
a.x(2)
window.resize()

Statement statement

Using the declaration statement, you can assign a value to the variable name.

example:

var i = 0
let j = 1
const k = 2

//declare an object value const car = { color: blue }

This is the function declaration:

//declare a function
function fetchFromNetwork() {
  //...
}
//or
const fetchFromNetwork = () => {
  //...
}

Control flow statement

You can use a statement to group statements:

{
  //this is a block
  const a = 1;
  const b = 2;
}

Using this syntax, as long as JavaScript requires one statement, there can be multiple statements.

Please note that any conditional control flow statement will check the expression and execute a statement or a block based on the expression:

if (condition === true) {
  //execute this block
} else {
  //execute this block
}

If there is only one statement, you can omit the curly braces:

if (condition === true) /* statement */ else /* another statement */

In the next section, I will introduce all the different control flow structures.

loop statement

The way the loop works is the same asifThe example above.

Some loops check an expression and execute a statement repeatedly until the value of the expression is true.

Others loop through the list and execute a statement (or block) for each element of the list until the list is complete.

See me fullJavaScript loop tutorial.

Miscellaneous statement

return <expression>

This statement returns a value from the function, thus ending the execution of the function.

throw <expression>

Throw an exception (we will see what an exception is later)

trywithcatch

The try / catch block is used to catch exceptions. Again, we will see those applied later.

try {

} catch (<expression>) {

}

use strict

This statement appliesStrict mode.

debugger

Add a breakpointDebuggercan use.

Download mine for freeJavaScript beginner's manual


More js tutorials: