JavaScript Statements
Statements are the building blocks of every JavaScript program, the keywords that make decisions, repeat work and structure your code. From if...else and for loops to function, class and async/await, here are all 24, each with its syntax and live, editable examples.
| Name | Description |
|---|---|
| async function | Declares a function that returns a promise and can use await for asynchronous code. |
| await | Pauses an async function until a promise settles, then resumes with its resolved value. |
| break | Immediately exits the nearest loop or switch statement. |
| class | Defines a blueprint for creating objects, with a constructor, methods and inheritance. |
| const | Declares a block-scoped constant that cannot be reassigned. The recommended default. |
| continue | Skips the rest of the current loop iteration and moves to the next one. |
| do...while | A loop that runs its body first and then checks the condition, so it always runs at least once. |
| export | Makes values from a module available for other files to import. |
| for | Repeats a block of code a set number of times using a counter you control. |
| for...in | Loops over the enumerable keys of an object. Not recommended for arrays. |
| for...of | Loops over the values of an iterable such as an array, string, Map or Set. |
| function | Declares a reusable block of code that can take inputs and return a value. |
| function* | A function that can pause and resume, producing a sequence of values one at a time. |
| if...else | Runs a block of code when a condition is true, with optional else and else if branches. |
| import | Brings exported values from another module into the current file. |
| let | Declares a block-scoped variable that can be reassigned. The default choice with const. |
| return | Ends a function and sends a value back to the code that called it. |
| switch | Compares a value against several cases and runs the matching block. Don't forget break. |
| this | A keyword whose value is the object a function is called on; it depends on how the function is invoked. |
| throw | Raises an error, stopping normal execution and jumping to the nearest catch block. |
| try...catch | Runs code that might fail and catches any error so the program can recover gracefully. |
| var | The old way to declare a variable. Function-scoped and hoisted: prefer let and const. |
| while | Repeats a block of code as long as a condition stays true. |
| yield | Pauses a generator function and produces a value to the caller. Resumes on the next request. |