JavaScript 1 1

What is 1 1 in JavaScript?

In JavaScript, the expression 1 + "1" involves two operands: the first being the number 1 and the second being the string "1". Given that the second operand is a string, JavaScript will execute string concatenation, resulting in the output of 11.

Essentially, in JavaScript, the behavior of the + operator varies based on the types of the operands it interacts with. This signifies that if either one or both of the operands are strings, the + operation will execute string concatenation rather than performing numeric addition.

In straightforward terms, when an expression contains a single string operand, JavaScript will automatically convert the remaining operands into strings and execute the concatenation process.

For example

Example

console.log( 1 + "1"); // output = 11

Output:

Consider the expression 1 - "1", where the subtraction operator (-) is not applicable to string values. In this case, if we attempt to execute the arithmetic operation involving a string, JavaScript will automatically convert the string into a numerical value.

In straightforward terms, JavaScript interprets the expression 1 - "1" as both operands being suitable for numerical calculations. Consequently, JavaScript automatically transforms the string "1" into the numerical value 1, enabling the subtraction to occur, which results in 0.

Example

Example

Console.log (1 - "1"); // output = 0

Output:

What is String Concatenation?

In JavaScript, concatenation refers to the method of adding one string to the conclusion of another string. This is achieved through the use of the + operator. When string literals and string constants are concatenated, this operation takes place during compile time, meaning that no concatenation will happen during run-time. Conversely, for string variables, concatenation occurs solely at run-time.

In straightforward terms, string concatenation refers to the method of combining two or more strings to form a new string.

How to concatenate strings in JavaScript?

In JavaScript, various techniques enable us to join strings and produce a new string as a result. Below are some methods commonly employed for string concatenation:

Using the String concat method

In JavaScript, the concat method is a native function associated with strings. The string concat method accepts one or multiple arguments, each representing different strings, and produces a new string that combines them.

To put it differently, the string concat method is utilized to merge two or more strings while leaving the original string unaltered, and it produces a new string as a result.

Syntax

Example

string.concat (value1, value2, ……, value n)

Example

Example

let str = 'Java';
let value = str.concat('T', 'point');
console.log (value);

Output:

Using JavaScript + operator

In JavaScript, the + operator allows for the addition of two strings, resulting in a concatenated string. A straightforward method for string concatenation in JavaScript involves utilizing the "+" operator to generate a new string or the "+=" operator to append to an already existing string.

For example

Example

let str1 = 'Cristiano';
let str2 = ' Ronaldo';
let str3 = '(CR7)';
let value = str1 + str2+ str3;
console.log(value);

Output:

Array join method

In JavaScript, the Array join function is utilized to merge all items within an array into a single string. By default, the elements are separated by a comma (,). Nevertheless, it is possible to define an alternative separator to be employed. It is crucial to understand that the join function does not alter the original array.

If we are developing a new string for concatenation, we can easily append that defined array and utilize the join method once more.

Example

Example

let arr = ['Hello', 'World'];
let str = arr.join('');
console.log (str);

Output:

Using the Template Literals

In JavaScript, what are referred to as template literals are commonly known as template strings. This feature, introduced in ES6, provides a modern approach to string concatenation. By utilizing template literals, which are defined using a dollar sign followed by curly braces, one can improve the expression that is included within a string.

Syntax

Example

Let variable = 'string';
`string text ${variable} string text`

In the structure of template literals, by utilizing the dollar sign ($) in conjunction with curly braces, we can effortlessly incorporate a variable into our string by enclosing it within back-ticks and signaling string concatenation.

Example

Example

let str1 = "Learning String";
let str2 = "Concatenation";
let str3 = "in JavaScript";
let main_str = `${str1} ${str2} ${str3} using Template Literals`;
console.log (main_str);

Output:

Input Required

This code uses input(). Please provide values below: