Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java script string comparison [duplicate]

I took a JS course on a website , and in one of the lessons there was a piece Of code that did not make sense to me :

enter image description here

the code is in the picture , why str1 is less then str2 ?

like image 435
Khirdzz Avatar asked Apr 21 '26 03:04

Khirdzz


1 Answers

Strings are compared based on standard lexicographical ordering, using Unicode values. That means "a" < "b" and "c" > "b"

Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. source

var str1 = "aardvark";
var str2="beluga";
console.log(str1 < str2);//true
console.log(str1.length < str2.length);//false
like image 175
Vladu Ionut Avatar answered Apr 23 '26 16:04

Vladu Ionut