else-if condition in if-else statements: Concatenation in JavaScript :

 

  • else-if condition in if-else statements:

While specifying conditions for if-else statements, there may arise a need to include more than just two scenarios. For example, suppose we have a variable ‘n’ and we have to check if it is positive or negative. JS code for this problem would look like this:

Quite simple, right?

However, suppose we twist this problem a bit. If we have to check if the value of ‘n’ is positive, negative, OR zero. Can we do it using only if-else? 

No, right?

Here comes the scenario where we have to check more than two conditions and give output for each. This is where else-if comes into the game. 

Structure of if else-if else looks something like this:

If (condition 1) 

{ statements }

else if (condition 2)

{ statements }

else 

{ statements }

 

  • Working for the above code is explained as follows:
    • Condition 1 is checked first. If it is true, statements inside the first block are executed and the remaining conditions are not checked. If the condition1 is false, the statements inside the first block are skipped and condition2 is checked.
    • A similar thing happens while checking condition2 also. If it is true, statements inside the second block are executed and the remaining conditions are not checked. If condition2 is false, statements inside the third block after the else are executed.

 

Coming back to our problem of finding if the value of a variable is positive, negative or zero, the code would look like this:

 

NOTE: There can be multiple else-if statements. Example:


  • Concatenation in Javascript:

Concatenation means joining multiple things. It is also used in the programming world a lot. It is a very common requirement to join two strings. 

For example, suppose there are two strings, firstName and lastName, and we want the full name of the person. In this case, we would want to join both the strings and produce a single one.

In Javascript, we have a predefined method concat(), which joins two or more strings. It is important to note that it does not replace the existing strings but return a new merged one.

 

The syntax of concat is:

string1.concat(string2,string3,.....);

It will return the merged string.

 

Example:

We can also insert spaces in between the combined strings:

Addition Operator

In addition to this, we can also use the + operator (addition operator). The addition operator (+) adds numbers, and also performs string concatenation.

Syntax -

let x = 5;

let y = 10;
let z = x + y;

This piece of code would simply give the output to be 15 i.e, the sum of two numbers as displayed above.

For concatenation: it first coerces both operands to primitives. The other operand is then transformed to a string and concatenated if one side is a string. If not, both sides are transformed into numbers, and a numeric addition is then carried out.

console.log(5 + 10);

// expected result: 15

 

console.log('hello ' + 'World');

// expected result: "hello World"

 

console.log(26 + '' November”);

// expected result: "26 November" - It will also include the space before November.



Comments

Popular posts from this blog

Day 9: CSS - Font Formate

Day 27: Css Variable