Why does JSHint argue against bitwise operators? How should I express this code?

javascript, jshint, jslint

Solution

Put

/*jshint bitwise: false*/

in the top of your file

A list of available options: http://jshint.com/docs/options/

Problem

I am using this bit of JavaScript to generate a UID: (original:) ``` //If ID has not been defined then generate a new unique ID. if(!id){ id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } ``` (formatted so it can be read:) ``` // If ID has not been defined then generate a new unique ID. if (!id) { id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( /[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); } ); } ``` JSHint does not like the use of bitwise OR and AND operators. I was wondering how I could rewrite this to be more 'standard friendly.' EDIT: JSHint states: ``` Line 8: id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); Unexpected use of '|'. Line 8: id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); Expected '===' and instead saw '=='. Line 8: id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); Unexpected use of '&'. Line 8: id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); Unexpected use of '|'. ```

Original source

Related problems