JavaScript Statements: Main Tips
- JavaScript statements are specific instructions used to inform the computer of the assigned task.
- Each statement sends a unique instruction.
- Each statement should end with a semicolon. It is not required but it is a good practice to keep your code organized.
What Statements Are
Basically, any JavaScript code consists of a variety of statements that guide a program or a website, and control their functionality. Writing JavaScript code without such functions is impossible: statements specify the actions the script will perform. Without them, you would have a bunch of elements with nothing to do.
Many programming languages require developers to end their statements with semicolons, but JavaScript gives them a choice. Anyway, even though JavaScript statements do not require the usage of semicolons, it can help you manage your code better.
The code example below shows one of the most simple JavaScript statements. It will write a message on the screen:
It's crucial that you don't mistake statements for expressions. The discussion of expression vs statement usually mentions that statements are lines of code that determine the sequence of actions, while any piece of code, evaluating to a value, can be identified as an expression.
List of Identifiers
When writing your code, you should keep in mind that JavaScript statement identifiers cannot be set as variable or function names. JavaScript statements begin with identifiers and end with other components such as arguments.
In other words, identifiers specify the function to be performed. They belong to the group of reserved words in JavaScript.
All JavaScript statement identifiers can be found in the table below:
Keyword | Description |
---|---|
break | Ends a loop command. |
continue | Ends and restarts a loop command. |
debugger | Stops JavaScript from running and calls the debugging function, if possible. |
do ... while | Runs and loops a block of code until a condition is no longer true. |
for | Loops a block of code until a specified condition is no longer true. |
for ... in | Specifies the code to execute for every element in a specified object. |
function | Defines a function. |
if ... else | Checks a specified condition and executes a block of code if it is true. |
return | Returns a specified value and exits the current function. |
switch | Sets multiple conditions and blocks of code. |
throw | Generates an error. |
try ... catch | Defines an error handling block of code. |
var | Declares a new variable. |
while | Loops a block of code while a specified condition is true. |