Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In coffeescript, what's the difference between "is not" and "!="?

Tags:

coffeescript

I've got this line of code:

console.log "source = #{@source.alignment} unit = #{unit.alignment}: " + (@source.alignment is not unit.alignment)

This is printing this out to the console:

source = good unit = bad: false

Why is it printing "false"? Shouldn't it be printing "true"? Logically, good "is not" bad.

This

console.log "source = #{@source.alignment} unit = #{unit.alignment}: " + (@source.alignment != unit.alignment)

prints

source = good unit = bad: true

as expected.

What's the difference? When should I use is not?

like image 935
Daniel Kaplan Avatar asked Jun 21 '13 01:06

Daniel Kaplan


People also ask

Is CoffeeScript better than JavaScript?

Easy Maintenance and Readability: It becomes easy to maintain programs written in CoffeeScript.It provides the concept of aliases for mostly operators that makes the code more readable.

Is CoffeeScript deprecated?

With the rise of ES6/7 and TypeScript, it seemed like CoffeeScript would become obsolete. But it's not. CoffeeScript was one of the pioneers of the compile-to-JavaScript concept. In fact, some of things you now see in the modern JavaScript appeared in the first version of CoffeeScript some 8 years ago.

Do people still use CoffeeScript?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).


1 Answers

It's an operator precedence issue:

a is not b => a is (not b)

That means that this compiles to the next js:

a === !b

In your case, b is unit.alignment, and as that var exists and its value is not falsy, !unit.alignment returns false

To solve your problem, check out isnt operator in Coffeescript docs

like image 143
Juan Guerrero Avatar answered Oct 21 '22 10:10

Juan Guerrero