Home >Web Front-end >JS Tutorial >How to determine whether a specified value is an even number in javascript
In JavaScript, you can use the "%" operator to divide the specified value by 2. If it can be divided, that is, the remainder is 0, it is an even number; the syntax is "if (value % 2 == 0) {console.log(value "is an even number");}".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
An even number is an integer that is divisible by 2.
According to this feature, we can use the "%" operator to divide the specified value by 2 (value % 2
). If it can be divided evenly, the remainder will be 0 is an even number.
Implementation code:
var value=4; if (value % 2 == 0){ console.log(value+" 是偶数"); }
Output result:
javascript advanced tutorial 】
The above is the detailed content of How to determine whether a specified value is an even number in javascript. For more information, please follow other related articles on the PHP Chinese website!