How to compare Strings in JavaScript-Code Examples

Radu TM

How to compare Strings in JavaScript

JavaScript by Radu TM June 21, 2022

var str1 = 'I ❤️ JavaScript';
var str2 = 'I ❤️ Java';

// use thelocaleCompare() method to compare strings in a locale-sensitive way:
console.log(str1.localeCompare(str2)); // 1

// use the < and > operators to compare strings in a alphabetical way:
console.log(str1 < str2); // false
console.log(str1 > str2); // true

// use the === operator to compare strings in a strict way:
console.log(str1 === str2); // false

0

14.739

How to compare Strings in JavaScript

JavaScript by Radu TM June 21, 2022

//declare two variables and assign them string values
let myString1 = 'I ❤️ JavaScript';
let myString2 = 'JavaScript is amazing';

//use the '===' comparison operator to compare the two strings
console.log(myString1 === myString2); //returns false

//use the '!==' comparison operator to compare the two strings
console.log(myString1 !== myString2); //returns true

0

14.739

How to compare Strings in JavaScript

JavaScript by Radu TM June 21, 2022

// to compare two strings in JavaScript, you can use the '===' operator
let str1 = '🍎';
let str2 = '🍎';

// the '===' operator will return true if the two strings have the same value
console.log(str1 === str2); // true

0

14.739

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories