Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript compares whether strings are equal

Shulou Source: shulou.com Published: 2022-06-03 17:02:41 11月13日 Update

This article mainly introduces javascript how to compare whether the string is equal, the article is very detailed, has a certain reference value, interested friends must read it!

Javascript compares whether the string is equal: 1, use the "= =" operator for equality comparison, syntax "str1==str2"; 2, use the "=" operator for equality comparison, syntax "str1===str2".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript compares whether strings are equal

Method 1: use "=" for equality comparison:

Var str1= "123456"; / / string var str2 = "123456"; / / string alert (str1==str2); / / print out true, that is, equal

Method 2: use "=" for equality comparison

Var str1= "123456"; / / string var str2 = "123456"; / / string alert (str1===str2); / / print out true, that is, equal

Extended knowledge: the difference between "=" and "= ="

"=" means "equivalent" and type conversions of the necessary values are performed before equality comparisons are made. To put it simply, it is to convert the value to the same type before making an equal comparison. Even if the type of the compared value is different, it can be cast to the same type without error.

Var str1= 123456; / / Integer var str2 = "123456"; / / string alert (str1==str2); / / print out true, that is, equal

"= =" means "identity" and no type conversion is performed, so if the two values are not of the same type, it returns false when compared. If you compare two variables whose types are not compatible with each other, a compilation error will occur.

Var str1= 123456; / / Integer var str2 = "123456"; / / string alert (str1===str2); / / print out false, that is, unequal or above is all the content of the article "how javascript compares whether strings are equal". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Character string type method same two content knowledge article grammar operator error operation necessity value interest variable can pass boy buddy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Shulou Information macOS vpn