Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint doesn’t expect my tildes

JSLint insists that my use of the somewhat exotic tilde operator in the below example is unexpected. What I’m wondering is whether this is a limitation of JSLint? or strict mode? or what else am I missing?

(function () {
    'use strict';
    if (~'foo'.indexOf('bar')) {
        return 'wild accusations';
    }
}());

Also, why shouldn’t I use the simple-looking tilde operator instead of the more complex example below? Surely there must be a good reason not to?

if ('foo'.indexOf('bar') >= 0) { … }
like image 850
Daniel Avatar asked Feb 27 '12 22:02

Daniel


Video Answer


1 Answers

From the JSLint Docs:

Bitwise Operators

JavaScript does not have an integer type, but it does have bitwise operators. The bitwise operators convert their operands from floating point to integers and back, so they are not as efficient as in C or other languages. They are rarely useful in browser applications. The similarity to the logical operators can mask some programming errors. The bitwise option allows the use of these operators: << >> >>> ~ & |.

You can enable it under options

Cheers

like image 163
Madbreaks Avatar answered Oct 05 '22 17:10

Madbreaks