|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "bitwise" |
| 5 | + ], |
| 6 | + "slugs": [ |
| 7 | + "option-bitwise" |
| 8 | + ], |
| 9 | + "linters": [ |
| 10 | + "jslint", |
| 11 | + "jshint" |
| 12 | + ], |
| 13 | + "author": "jallardice", |
| 14 | + "subject": "option" |
| 15 | +} |
| 16 | +--> |
| 17 | + |
| 18 | +### What does this option do? |
| 19 | + |
| 20 | +In JSLint the `bitwise` option is used to allow the usage of any bitwise |
| 21 | +operators. In JavaScript the available bitwise operators are `<<` (bitwise left |
| 22 | +shift), `>>` (bitwise right shift), `>>>` (unsigned bitwise right shift), `&` |
| 23 | +(bitwise AND), `|` (bitwise OR), `^` (bitwise XOR) and `~` (bitwise NOT). In the |
| 24 | +following example we are using the bitwise OR operator to round a number down to |
| 25 | +the closest integer which is a relatively common shorthand trick: |
| 26 | + |
| 27 | +<!--- |
| 28 | +{ |
| 29 | + "linter": "jslint" |
| 30 | +} |
| 31 | +--> |
| 32 | +```javascript |
| 33 | +/*jslint bitwise: true */ |
| 34 | +var x = 1.2345 | 0; |
| 35 | +``` |
| 36 | + |
| 37 | +The JSHint `bitwise` option is used to **disallow** the use of those operators. |
| 38 | +Here's the same example again: |
| 39 | + |
| 40 | +<!--- |
| 41 | +{ |
| 42 | + "linter": "jshint" |
| 43 | +} |
| 44 | +--> |
| 45 | +```javascript |
| 46 | +/*jshint bitwise: true */ |
| 47 | +var x = 1.2345 | 0; |
| 48 | +``` |
| 49 | + |
| 50 | +### When should I use this option? |
| 51 | + |
| 52 | +With JSLint, if the `bitwise` option is not set, you'll get an "Unexpected |
| 53 | +'{a}'" error, where "{a}" is a bitwise operator, any time a bitwise operator is |
| 54 | +used. In JSHint the opposite is true and you'll receive an "Unexpected use of |
| 55 | +'{a}'" error for each bitwise operator occurence when the option is set. |
| 56 | + |
| 57 | +If you require the use of bitwise operators for actual program logic then you |
| 58 | +cannot enable this option. However, if you do not need to use such operators and |
| 59 | +want to prevent tricks such as the one shown above, enabling this option is a |
| 60 | +good way to do so. |
| 61 | + |
| 62 | +Note that in JSHint this is an *enforcing* option which means JSHint does not |
| 63 | +apply it by default. If you do not explicitly set this option to `true` JSHint |
| 64 | +will allow the use of bitwise operators anywhere in your code. |
| 65 | + |
| 66 | +#### Recommendation |
| 67 | + |
| 68 | + - **JSLint** - Set this option to `true` (you will be able to use bitwise |
| 69 | + operators). |
| 70 | + |
| 71 | + - **JSHint** - Do not set this option (you will be able to use bitwise |
| 72 | + operators). |
0 commit comments