Open In App

JavaScript Statements

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript statements are made of: Values, Operators, Keywords, Expressions, and Comments. JavaScript statements are executed in the same order as they are written, line by line.

Examples of JavaScript Statements

Semicolons

  • Semicolons separate JavaScript statements.
  • A semicolon marks the end of a statement in JavaScript. 

Example: In this example, we have shown the use of Semicolons.

Javascript
let a, b, c;
a = 2;
b = 3;
c = a + b;
console.log("The value of c is " + c + ".");

Output
The value of c is 5.

Multiple statements on one line are allowed if they are separated with a semicolon.

a = 2; b = 3; z = a + b;

Code Blocks

JavaScript statements can be grouped together inside curly brackets. Such groups are known as code blocks. The purpose of grouping is to define statements to be executed together. 

Example: In this example, we have shown Code Blocks. 

Javascript
function myFunction() {
    console.log("Hello");
    console.log("How are you?");
}

myFunction()

Output
Hello
How are you?

White Space

JavaScript ignores multiple white spaces.

Example: In this example, we have shown that JavaScript ignores white spaces.

Javascript
console.log(10*2);
console.log(10  *  2);

Output
20
20

Both the result will be the same.

Line Length and Line Breaks

JavaScript code’s preferred line length by most programmers is up to 80 characters. The best place to break a code line in JavaScript, if it doesn’t fit, is after an operator

Example: In this example, we have shown Line Length and Line Breaks

document.getElementById("geek1").innerHTML = "Hello Geek!";

Keywords

Keywords are reserved words and cannot be used as a variable name. A JavaScript keyword tells about what kind of operation it will perform. 

Some commonly used keywords are:

Keyword

Description

var

Used to declare a variable

let

Used to declare a block variable

const

Used to declare a block constant

if

Used to decide if certain bloc will get executed or not

switch

Executes a block of codes depending on different cases

for

Executes a block of statements till the condition is true

function

Used to declare a function

return

Used to exit a function

try

Used to handle errors in a block of statements

break

Used to terminate a loop

continue

Used to continue a loop


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads