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
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics