Skip to content

Commit 533cb74

Browse files
author
James Allardice
committed
Finished article on JSHint bitwise operator
1 parent 70aa072 commit 533cb74

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

option-articles/bitwise-jshint.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
### What does this option do?
1818

19-
The JSHint `bitwise` option is used to disallow the usage of any bitwise operators.
19+
The JSHint `bitwise` option is used to disallow the usage of any bitwise
20+
operators. In JavaScript the available bitwise operators are `<<` (bitwise left
21+
shift), `>>` (bitwise right shift), `>>>` (unsigned bitwise right shift), `&`
22+
(bitwise AND), `|` (bitwise OR) and `^` (bitwise XOR). In the following example
23+
we are using the bitwise OR operator to round a number down to the closest
24+
integer which is a relatively common shorthand trick:
2025

2126
<!---
2227
{
@@ -25,7 +30,18 @@ The JSHint `bitwise` option is used to disallow the usage of any bitwise operato
2530
-->
2631
```javascript
2732
/*jshint bitwise: true */
28-
var x = 1 & 2;
33+
var x = 1.2345 | 0;
2934
```
3035

31-
### What error messages can it cause or prevent?
36+
### When should I use this option?
37+
38+
The use of the `bitwise` JSHint option will cause an "Unexpected use of '{a}'"
39+
error, where "{a}" is a bitwise operator, any time a bitwise operator is used.
40+
If you require the use of bitwise operators for actual program logic then you
41+
cannot enable this option. However, if you do not need to use such operators and
42+
want to prevent tricks such as the one shown above, enabling this option is a
43+
good way to do so.
44+
45+
Note that this is an *enforcing* option which means JSHint does not apply it by
46+
default. If you do not explicitly set this option to `true` JSHint will allow
47+
the use of bitwise operators anywhere in your code.

0 commit comments

Comments
 (0)