TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript Equals: 2, 3 Equals Signs

Use two equals and three equals comparisons with strings and numbers.
Equals. In JavaScript we can compare things with two equals signs or three equals signs. There is a subtle difference here. But in most cases the two have the same effect.Strings
With three equals signs, the types of the two variables must be equal. With two equals signs, a conversion may occur in the browser.
An example. Here we have a string that stores digit characters (text). And we have a number that directly stores its integer value.

If 1: The two variables are considered equal with the "==" operator as the string is converted to a number.

If 2: The two variables are not equal here as they have different types. The inner block is not reached.

If 3: This evaluates to true as the two values have unequal types. This operator tests both types and values.

JavaScript program that uses equality comparisons var text = "800"; var number = 800; // Use type conversion with two equals. if (text == number) { console.log("OK"); } // Do not allow type conversion with 3 equals. if (text === number) { console.log("Not reached"); } // Use not equals with no type conversion. if (text !== number) { console.log("OK 2"); } Output OK OK 2
In programming, we try to reduce the number of steps needed. The best practice remains as before: use three equals signs when possible. A possible conversion is eliminated.
A brief summary. With three equals signs, we compare both the type and the value of two variables. With two equals signs, only the values are compared—conversions may occur.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf