The Wayback Machine - https://web.archive.org/web/20240710154402/https://www.geeksforgeeks.org/javascript-statements/
Open In App

JavaScript Statements

Last Updated : 29 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 block 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


Previous Article
Next Article

Similar Reads

Control Statements in JavaScript
JavaScript control statement is used to control the execution of a program based on a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition. Types of Control Statements in JavaScriptConditional Statement: These statemen
3 min read
Conditional Statements in JavaScript
JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition. There are several methods that can be used to perform Conditional Statements in JavaScript. Conditional StatementDescr
7 min read
What is the use of curly brackets in the `var { ... } = …` statements ?
Destructuring assignment allows us to assign the properties of an array or object to a bunch of variables that are sometimes very convenient and short. Consider the following illustration. Both of the below-mentioned methods are right and produce the same result. Without Destructuring: var array = [1, 20, 40]; var first = array[0] var second = arra
2 min read
CoffeeScript | Statements
The syntax of CoffeeScript is simpler to JavaScript and can be easily learned if you have knowledge of JavaScript. It avoids the use of semicolons, curly braces, and variable declarations. CoffeeScript Statements: The statements of CoffeeScript do not end with semicolons (;). In this language, a new line is considered as a separate statement by the
3 min read
How to use if statements in Underscore.js templates ?
Underscore.js is a javascript library that provides helper functions in functional programming without extending the built-in objects. This package consists of 100 functions like map, filter, etc. We can install this by using npm. npm install underscore Syntax: const _ = require('underscore'); Approach: Write your markup code regarding the requirem
1 min read
How to use if, else and not statements in Ember.js ?
Ember.js is a framework for web development. It makes the process of development and prototyping faster with its tools. We can create dynamic web pages with it. Based on the dynamic data, we may or may not want to display some data or some UI or modify the UI. So we will learn to use If, Else and Not in an Ember.js application. Create the project S
3 min read
CoffeeScript Conditional Statements
The CoffeeScript programming language is used to write a simpler syntax that compiles into JavaScript before getting executed. In any programming language, conditions play an important role in the execution of code in a particular sequence. In this article, we will see various conditional Statements such as If, Else, Else If, etc. that CoffeeScript
5 min read
Simple way to condense if statements into a function to check arguments
In this article, we will try to understand how we use if statements inside a function in order to check the validity of the argument, and later we will see the simple way to condense(reduce) if statements into a function to check arguments as well as theoretical explanations in JavaScript. Let us first have a look into the following syntaxes (of ob
3 min read
TypeScript Interface vs Type Statements
In TypeScript, both "interface" and "type" statements are used to define the shape or structure of an object or a type. They provide a way to define custom types and enforce type checking in TypeScript code. However, there are some differences between the two. InterfaceInterfaces in TypeScript are used to define agreements for objects. They can con
3 min read
How to use conditional statements with EJS Template Engine ?
Conditional statements in EJS templates allow you to dynamically control the content that is rendered based on specified conditions. Similar to JavaScript, EJS provides a syntax for using conditional logic within your HTML templates. In this article, we will see a practical example of using conditional statements with the EJS template. What are Con
3 min read
three90RightbarBannerImg