Open In App

JavaScript String Operators

Last Updated : 23 Nov, 2024
Comments
Improve
Suggest changes
1 Likes
Like
Report

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);

Output
GeeksforGeeks

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);

Output
GeeksforGeeks

3. Comparison Operators

Equality (==) : Checks if two strings are equal (ignoring type).

JavaScript
let s1 = "gfg";
let s2 = "gfg";

console.log(s1 == s2);

Output
true

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); 

Output
true

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

Output
false

Inequality (!=) : Checks if two strings are not equal (ignoring type).

JavaScript
let s1 = "gfg";
let s2 = "ggg";

console.log(s1 != s2);

Output
true

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); 

Output
true

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); 

Output
true


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

Output
true
false
true
false
Suggested Quiz
4 Questions

What is the output of the following code?

let s1 = "Geeks";

let s2 = "forGeeks";

let res = s1 + s2;

console.log(res);


  • A

    Geeks forGeeks

  • B

    Geeks+forGeeks

  • C

    GeeksforGeeks

  • D

    Error

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;


  • A

    Reverses both strings

  • B

    Appends s2 to s1

  • C

    Multiplies the lengths of both strings

  • D

    Throws a type error

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);


  • A

    false

  • B

    true

  • C

    Syntax error

  • D

    undefined

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?

  • A

    ==

  • B

    !=

  • C

    ===

  • D

    <=

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