JavaScript String Operators
Last Updated :
23 Nov, 2024
JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string.
1. Concatenate Operator
Concatenate Operator in JavaScript combines strings using the '+' operator and creates a new string.
JavaScript
let s1 = "Geeks";
let s2 = "forGeeks";
let res = (s1 + s2);
console.log(res);
2. Concatenate Assignment Operator
We can modify one string by adding content of another one to it.
JavaScript
let s1 = "Geeks";
let s2 = "forGeeks";
s1 += s2;
console.log(s1);
3. Comparison Operators
Equality (==) : Checks if two strings are equal (ignoring type).
JavaScript
let s1 = "gfg";
let s2 = "gfg";
console.log(s1 == s2);
Here is an example to show that type checking is ignored.
JavaScript
let s1 = "gfg"; // Primitive Type
let s2 = new String("gfg"); // Object type
console.log(s1 == s2);
Strict Equality (===) : Checks if two strings are equal in value and type both.
JavaScript
let s1 = "gfg"; // Primitive Type
let s2 = new String("gfg"); // Object type
console.log(s1 === s2); // false
Inequality (!=) : Checks if two strings are not equal (ignoring type).
JavaScript
let s1 = "gfg";
let s2 = "ggg";
console.log(s1 != s2);
Here is an example to show that type checking is ignored.
JavaScript
let s1 = "gfg"; // Primitive Type
let s2 = new String("ggg"); // Object type
console.log(s1 != s2);
Strict Inequality (!==) : Checks if two strings are equal in value and type both.
JavaScript
let s1 = "gfg"; // Primitive Type
let s2 = new String("ggg"); // Object type
console.log(s1 !== s2);
Lexicographical Comparison (<, >, <=, >=): Compares strings based on Unicode values.
JavaScript
let s1 = "age";
let s2 = "bat";
console.log(s1 < s2); // true
console.log(s1 > s2); // false
console.log(s1 <= s2); // true
console.log(s1 >= s2); // false
Outputtrue
false
true
false
What is the output of the following code?
let s1 = "Geeks";
let s2 = "forGeeks";
let res = s1 + s2;
console.log(res);
Explanation:
The + operator concatenates two strings to form a new combined string.
What does the += operator do when used with strings in JavaScript?
let s1 = "Geeks";
let s2 = "forGeeks";
s1 += s2;
-
-
-
Multiplies the lengths of both strings
-
Explanation:
+= is the concatenate assignment operator; it modifies the existing string by adding another string to it.
What will be the result of the following code?
let s1 = "gfg";
let s2 = new String("gfg");
console.log(s1 == s2);
Explanation:
== checks only value, not type. Even though one is a primitive string and the other is a String object, their values match.
Which comparison operator checks both value and type when comparing strings?
Explanation:
=== is strict equality, meaning both type and value must match. A primitive string and a String object will not be equal using ===.
Quiz Completed Successfully
Your Score : 2/4
Accuracy : 0%
Login to View Explanation
1/4
1/4
< Previous
Next >
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics