DEV Community

Max Lockwood
Max Lockwood

Posted on • Originally published at maxlockwood.dev

What exactly are JavaScript Statements?

What exactly are JavaScript Statements?

JavaScript statements are commands that tell the browser what to do. Semicolons are used to separate statements (;). The JavaScript statement is the JavaScript code that is translated line by line by the browser.

What are the Four JavaScript Statements?

JavaScript statements are composed of:

  • Values
  • Operators
  • Expressions
  • Keywords and Comments.

This statement tells the browser to write “Hello World.” inside an HTML element with id=“demo”:

document.getElementById(demo).innerHTML = Hello World.;
Enter fullscreen mode Exit fullscreen mode

Most JavaScript programs contain a large number of JavaScript statements.
The statements are executed one by one in the order in which they were written.

Semicolons ;

Semicolons are used to separate JavaScript statements.
At the end of each executable statement, place a semicolon:

let a, b, c;  // Declare 3 variables
a = 10;        // Assign the value 10 to a
b = 5;        // Assign the value 5 to b
c = a + b;    // Assign the sum of a and b to c
// Output: c = 15
Enter fullscreen mode Exit fullscreen mode

When separated by semicolons, multiple statements on one line are allowed:

a = 10; b = 5; c = a + b;
Enter fullscreen mode Exit fullscreen mode

JavaScript White Space

Multiple spaces are ignored by JavaScript. You can make your script more readable by including white space.

The following lines are equivalent:

let person = "Tom";
let person="Tom";
Enter fullscreen mode Exit fullscreen mode

If you use a code formatter like Prettier in your code editor this will automatically fix this formatting issue for you when you save your file.

To improve readability, spaces should be placed around operators (= + - * /):

let x = y + z;
Enter fullscreen mode Exit fullscreen mode

JavaScript Line Length and Line Breaks

Code lines longer than 80 characters are often avoided by programmers for better readability.

If a JavaScript statement does not fit on one line, the best place to break it is after an operator:

document.getElementById("demo").innerHTML =
"Hello World!";
Enter fullscreen mode Exit fullscreen mode

JavaScript Code Blocks

JavaScript statements can be grouped together in code blocks by enclosing them in curly brackets {...}
Code blocks are used to define statements that will be executed together.
JavaScript functions are one place where statements are grouped together in blocks:

function greeting() {
  document.getElementById("demo1").innerHTML = "Hello Tom!";
  document.getElementById("demo2").innerHTML = "How are you?";
}
Enter fullscreen mode Exit fullscreen mode

For code blocks in this tutorial, I like to use two spaces of indentation. In your dedicated code editor, you can change the size to whatever you want.

JavaScript Keywords

JavaScript statements often start with a keyword to identify the JavaScript action to be performed.

Here is a list of some of the most common keywords:

Keyword Description
var Declares a variable
let Declares a block variable
const Declares a block constant
if Marks a block of statements to be executed on a condition
switch Marks a block of statements to be executed in different cases
for Marks a block of statements to be executed in a loop
function Declares a function
return Exits a function
try Implements error handling to a block of statements

Note: JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.

Conclusion

  • Javascript statements are commands that instruct the browser on what action to take.
  • Javascript statements can be used in addition to or instead of any Javascript object.
  • Semicolons are used to separate Javascript statements (;)
  • A single line can contain multiple javascript statements.
  • A javascript statement can span multiple lines.

Further reading

Want to learn more about statements? Then check out – Statements and declarations – JavaScript | MDN

See also

What is the Syntax of JavaScript?
What are Functions in JavaScript?
What are the Three Variables in JavaScript?

If you liked this article, then please share. You can also find me on Twitter for more updates.

Top comments (0)