Hi Folks, In this article we are going to learn about some basic concept of JavaScript like PEMDAS Rule, Template Literals and Semi-colon. This is the part2 of JavaScript Introduction, If you haven’t read part1 then I would suggest you to read if first by clicking of Part1.
PEMDAS Rule
In mathematics we follow BODMAS rule to solve any equation which has multiple operations. Similarly we have PEMDAS rule to solve expression in JavaScript.
B = Bracket O = Order D = Division M = Multiplication A = Addition S = Subtraction
PEMDAS
P = Parentheses E = Exponential M = Multiplication D = Division A = Addition S = Subtraction
JavaScript follow this order to solve expression which has multiple operations.
e.g.
let output = 10 * (2 + 5) + 2**2 /10 + 100 - 10;
console.log(output);
// Print
// 160.4
//---------------Let's Decode------------------
// output = 10 * (2 + 5) + 2**2 /10 + 100 - 10;
// Parenthesis = (2+5) = 7; ===> [10*7 + 2**2/10 + 100-10]
// Exponential = 2**2 = 4 ===> [10 * 7 + 4 / 10 + 100 - 10]
// Multiplication = 10 * 7 ===> [ 70 + 4 / 10 + 100 - 10]
// Division = 4 / 7 ===> [ 70 + 0.4 + 100 - 10]
// Addition = 70 + 0.4 + 100 ===> [170.4 - 10]
// Subtraction = 170.4 - 10 ===> 160.4
Template Literals (Template Strings)
If we define strings using backsticks(` `) is called Template Literals.
let Greet = `Hello`;
Why do we use backticks (` `) over quotes(“ “) to define strings?
Suppose we have to add multiple strings and expressions, then we will use below syntax.
let Str1 = "Hello";
let Str2 = "Bharat";
let Score1 = 50;
let Score2 = 50;
console.log(Str1 + " " + Str2 + " your Score is " + (Score1 + Score2));
// Print
// Hello Bharat your Score is 100
As we can see from above example to concatenate multiple strings we are using addition operator and double quotes again and again which is decreasing the readability of the code.
To avoid this, we uses backticks( ). Let’s see the similar example using backticks (` `).
let Str1 = "Hello";
let Str2 = "Bharat";
let Score1 = 50;
let Score2 = 50;
console.log(`${Str1} ${Str2} your Score is ${(Score1 + Score2)}`);
// Print
// Hello Bharat your Score is 100
Example 2:-
let Greet1 = "Hello"
let Greet2 = "Good Morning"
let Greet = `${Greet1} ${Greet2}`;
console.log(Greet);
// Print
// Hello Good Morning
Semi Colon in JS.
As it is not mandatory to use semicolon in JS but if we are writing multiple executing statements in a single line then we have to use it, just to make compiler to differentiate between them.
e.g.
//----------------------- --Without Semicolon-----------------------------
let Greet = "Hello" let name = "Bharat" console.log(`${Greet} ${name}`)
// This will throw an error
//--------------------------With Semicolon-------------------------------
let Greet1 = "Hello" ; let name1 = "Bharat"; console.log(`${Greet} ${name}`);
//It will Print [ Hello Bharat ]
To avoid semicolon, we have to write all executing statements in a separate line which is actually a general way of writing code.
let Greet = "Hello"
let name = "Bharat"
console.log(`${Greet} ${name}`)
// Print
// Hello Bharat
Note:-
Thank you so much for reading it completely, Go and get your hands dirty on it, try all the code by yourself.
If you liked it do like, comment, share and follow for more content. Comment for any feedback.