Troubleshooting how to use the good old == instead of ===

I really like Coffeescript, but one thing that has been driving me crazy lately is problems like numbers and strings in if statements. This is usually not a problem, because Javascript does not care when you use ==, but Coffeescript converts all comparisons to ===. Is there any way to return the messy old == comparisons? I feed nonsense, but I did not find anything on it.

The reason for this is that I converted the code of other people using the brilliant http://js2coffee.org/ to make it easier to read, but then I introduce type problems like == comparisons are replaced with ===. Needless to say, I will be lazy to reorganize the entire code;).

+6
source share
2 answers

As asawyer said, this is by design. If you really think you need a == comparison, then you can put it in backticks:

 if `foo == bar` alert 'Sloppy comparison true' 
+10
source

This is by design. Quote from CoffeeScript: JavaScript Accelerated Development

CoffeeScripts and == are both compiled into JavaScripts ===; there is no way to get free JavaScripts == type checking which was frowned by JSLint and others as the source of many "WTF?" moments. Let's take an example from http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array :

 ",,," == new Array(4) // true There are also cases where == isn't transitive: '' == '0' // false 0 == '' // true 0 == '0' // true 

To avoid these heads, you must perform type conversions explicitly

+11
source

Source: https://habr.com/ru/post/910083/


All Articles