Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint reports unexpected use of '&' and '|' -- I'd like to clean this

I'm trying to get my Javascript code 100% JSLint clean.

I've got some JS code that I've lifted from elsewhere to create a UUID. That code has the following line:

s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);

This line incites JSLint to generate two error messages:

1) Unexpected use of '&'
2) Unexpected use of '|'

I don't understand why -- I'd appreciate counsel regarding how to recode to eliminate the error message.

like image 874
Zhami Avatar asked Jun 14 '10 18:06

Zhami


2 Answers

The reason "why" is that actual bitwise operations are exceedingly rare in JS, and those operators appearing in JS code almost always are a typo for the boolean versions (&&, ||). That's why JSLint cares. This is a legit use of bitwise ops though. I believe you can silence the warning with the bitwise flag:

/*jslint bitwise: true */
like image 64
Ben Zotto Avatar answered Nov 14 '22 22:11

Ben Zotto


Did you give it the bitwise option? That option warns on all uses of bitwise operations, as they tend to be inefficient in Javascript (the native floats need to be converted to ints for the bitwise operation, and then converted back)

like image 25
Michael Mrozek Avatar answered Nov 14 '22 23:11

Michael Mrozek