JavaScript Statements
Statements are instructions that a JavaScript program execute.
They form the building blocks of JavaScript.
JavaScript statements direct the flow and actions of a JavaScript program.
This chapter lists the types of JavaScript statements.
Variable Declarations
let
Declares a block-scoped local variableconst
Declares a block-scoped, read-only variablevar
Declares a function-scoped variable (not preferred in modern JavaScript)
Assignment Statements
Assignment statements assign values to variables using an assignment operator.
- Examples:
let x = y
,let x = 10
Expressions
- Literals
100, "Hello", true, false, undefined
- Arithmetic expressions
5 + 3, x * y, 10 / 2
- String expressions
"hello" + " world", text.toUpperCase()
- Logical expressions
a > b, x === y && z, !isValid
- Function calls
myFunction(), console.log("message")
- Function expressions
let func = function() { /* ... */ };
- Array initializers
[1, 2, 3], ["Apple", "Banana", "Orange"]
- Object initializers
{ key: "value" }
Conditional Statements
if...else
Executes different blocks of code based on a conditionswitch
Evaluates an expression and executes code blocks based on matching case values
Looping Statements
for
Defines a loop that executes a block of code a number of timeswhile
Defines a loop that executes a block of code while a condition is truedo...while
Similar to while, but guarantees at least one execution of the loopfor...in
Defines a loop over the enumerable properties of an objectfor...of
Defines a loop over an iterable object (arrays, strings, etc)
Function Definition Statements
function
Declares a named function- Arrow function (
=>
)
A shorter syntax for defining a function expressions.
Class Definition Statements
class
Defines a JavaScript class
Module Statements
import
Imports bindings exported by another moduleexport
Exports functions, objects, or primitive values from a module
Error Handling Statements
try...catch
Defines a code block for handling potential errorsthrow
Throws a user-defined exception
Other Statements
break
Terminates a loop or switchcontinue
Skips the current iteration of a loop and proceeds to the nextreturn
Specifies the value to be returned by a functiondebugger
Invokes any available debugging functionality