Statements in JavaScript

Any programming language have certain fundamental identities which come together in form of a program to do something. Similarly JavaScript have statements as those identities which execute to make something happen.

JavaScript Statement Vs Expression?
Whilst JavaScript Expressions are evaluated to produce a value, JavaScript Statements are executed to make something happen. JavaScript statements are often terminated by semicolon(;)

If in case, your not aware of What JavaScript Expressions are? Then you don’t need to worry as I’ve put together a complete article explaining everything about these, you can see that article here – JavaScript Expressions.

Moreover in just a few words, it could also be said that JavaScript code is just sequence of statements which JavaScript Engine execute.

JavaScript Expression Statements

Wherever JavaScript expects a statement, you can also write an expression. Such a statement is called an expression statement. The reverse does not hold: you cannot write a statement where JavaScript expects an expression. For example, an if statement cannot become the argument of a function.

Let’s understand this with an example –
Math.sin(x); // This is an expression meaning sin of x
a = Math.sin(x); // This can be considered as Expression Statement as a is being assigned some value(Statement means some action)

Compound Statements in JavaScript

Code blocks which have many statements grouped together as a single block of code are known as Compound Statements. These are often used for merging together functionality of single statements and treat it as single entity in code.

{
	name = "Tesla";
	model = "Y";
}
// This is a code block (Compound Statement)
// Use can assign this to a variable and later use in the code

var car;
car = {
	name = "Tesla";
	model = "Y";
}
// Now car is a single variable, but internally have multiple statements

Few things that a programmer need to keep in mind about Compound Statement are –
• Compound Statement doesn’t end with semicolon, rather statements inside it does.
• Compound Statement can be treated as a single entity by assigning it to some variable.

Compound statement helps JavaScript Developer to abstract out code complexity, make code simpler by refactoring it into simple blocks. There does exist opposite of Compound statement, which is Empty Statement.

An Empty Statement doesn’t have any statement, it’s statement less. Empty statement is just a semicolon(;) with no or empty body. These could be used for Control flow in code, specifically with loops like For Loop.

for (i = 0; i < a.lenght; a[i++] = 0);
// This loop doesn't have any body rather have empty statement(;)

Note: – Be careful while using empty statement in JavaScript as placing semicolon at end of wrong code line may affect your code a lot. For example –
if (a == b);
console.log(“Computer Science Hub”);

if (a == b)
console.log(“Computer Science Hub”);

Above two pieces of code are completely different for first one ‘Computer Science Hub’ will be printed to console in all cases. But for second one it will only be printed if a,b have same value.

Declarations Statements in JavaScript

Declarations statements declare/define either a variable(with var keyword) or a function(with function keyword). These statements are just used for declaring variable names or functions.

var car = { name:"Tesla", model: "S", speed: "60"}; 
var driving = function(x) { return car.speed};

Here car, driving are both declarations of variables 
whilst function(x) is a function declearation statement

If your interested in knowing more about Variables in JavaScript, then you can check out this awesome other article – JavaScript Variables.

Table Summarising all of JavaScript’s Statements with their Functionalities

Statement nameSyntax of StatamentFunctionality
breakbreak; or break label;Exit from enclosing loop/switch statement or shifting to labelled object name
casecase expression;Used within JavaScript’s switch statements
continuecontinue; or continue label;Skipping over lines of code and starting again executing code from top of loop
defaultdefualt :Default code block of switch statement
whilewhile(expression) statementJavaScript Loop
See examples of while loop here – JavaScript while loop
do whiledo {
statement
} while (expression)
Kind of while loop
empty; (Just a semicolon)Do nothing
forfor(initialize; testing expression; updating){
statement
}
For doing some code statements repetitively
for in for (var i in object) statementEnumerate the properties of JavaScript Objects
functionfunction name(parameters){
statements
}
Treating number of code statements just as a single entity
use strict“use strict”; Certain JavaScript restrictions
if elseif (testing expression){
statement1
}
else{
statement2
}
Making choice out 2 code blocks of which one to execute
labellabel : statementAssigning some code block a name.
You can see an example of this here – Labelled Statements in JavaScript.
varvar name;
var name = value;
Declaring/Initialising a variable
returnreturn; or return expression;Return a value from a function
switchswitch (expression) {
statement
}
Executing certain code blocks based upon testing expression value.
See example of JavaScript switch statement here – JavaScript Conditional switch statement.
throwthrow expression;For throwing an error typically uses JavaScript’s Error class
trytry { statements }For handling exceptions
Table containing common JavaScript keywords with their functionalities

Conclusion

Couple of things need to kept in mind while using Statements in JavaScript code, I’ve covered all of those above. In case you don’t understand something completely or may be have some question then please comment down below, I will try to reply as soon as possible.

Moreover if your learning JavaScript Programming Language then I’ve put together number of amazing articles on JavaScript on this website, you can check those out here – JavaScript Computer Science Hub.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts