JavaScript Statements & Expressions Explained

JavaScript Statements & Expressions Explained

ยท

2 min read

We have seen some great topics like map, filter, reduce and much more in the previous blogs, So here is a simple concept in JavaScript ๐Ÿ˜‚

Expression

Expression is a piece of code that produces a value.

Here is a small example,

// These are expression and it is gonna produce a value
3 + 4  
6 - 3
4 / 2
true && false && !true

JavaScript has the following expression categories

String Expressions:

String expressions are expressions that evaluate a string.

'hello';

Logical Expression

Expressions that evaluate the boolean value true or false are considered to be logical expressions.

10>4
10<5

Left-hand-side Expressions: Left-hand-side expressions are those that can appear on the left side of an assignment expression.

// variables such as a
a =10;
// elements of arrays
b[3] = 3;

Assignment Expressions: When expressions use the = operator to assign a value to a variable, it is called an assignment expression.

b = 32;
age = 43;
str = `You are dumb ๐Ÿ˜‚`

Statements

It is a bigger piece of code that is executed but doesn't produce a value

if(5>4) {
 const str = `5 is bigger` 
}

In the above code, it doesn't produce any value it simply declares the str variable. This is completely different from having 3 + 4 .

Declaration Statements:

Such type of statements create variables and functions by using the var and function statements respectively.

 function greet(message) => {console.log(message);}

Expression Statements:

Wherever JavaScript expects a statement, you can also write an expression. Such statements are referred to as expression statements. But the reverse does not hold. You cannot use a statement in the place of an expression.

// results in error as you can pass only expressions as a function argument
console.log(var a);

Conditional Statements:

Conditional statements execute statements based on the value of an expression. Examples of conditional statements include the if..else and switch statements.

// Ignore the format ๐Ÿ˜‚
if (expression) {
    statement 1 }
else {
    statement 2 }

Thanks for reading my blog & Let's connect! ๐Ÿ™Œ๐Ÿผ

Follow me for more similar articles :D

Feel free to subscribe to my Youtube Channel & Follow me on Twitter <3

ย