Use the unary +
operator, which converts its operand into a number.
+ true; // 1
+ false; // 0
Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says.
More Related Contents:
- Generate unique random numbers between 1 and 100
- How to print a number with commas as thousands separators in JavaScript
- Does JavaScript support 64-bit integers?
- Check if a number has a decimal place/is a whole number
- How does x|0 floor the number in JavaScript?
- Extremely large numbers in javascript
- Can I hide the HTML5 number input’s spin box?
- How can I extract a number from a string in JavaScript?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript
- What’s the best way to convert a number to a string in JavaScript?
- How can I convert string to datetime with format specification in JavaScript?
- Is && statement() the same as if() statement()?
- how to display a javascript var in html body
- When to use the double not (!!) operator in JavaScript
- HTML input type=”number” still returning a string when accessed from javascript
- How to convert a String containing Scientific Notation to correct Javascript number format
- Are there are any side effects of using this method to convert a string to an integer
- In javascript, is an empty string always false as a boolean?
- Round number up to the nearest multiple of 3
- Javascript: Words to numbers [closed]
- Converting string “true” / “false” to boolean value [duplicate]
- Prime Numbers JavaScript
- Why does Array.filter(Number) filter zero out in JavaScript?
- How to format a number with commas as thousands separators?
- JavaScript adding a string to a number
- Convert integer into its character equivalent, where 0 => a, 1 => b, etc
- Show a leading zero if a number is less than 10 [duplicate]
- Integers in JavaScript
- Convert css width string to regular number
- Print / display a JavaScript variable’s name instead of it’s value