Code has been added to clipboard!

Statements

Reading time 3 min
Published Aug 9, 2017
Updated Oct 2, 2019

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:

Example
document.write("Hello there");

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.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen