Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What will happen when 1 is converted to Boolean in JavaScript?
In JavaScript, when the number 1 is converted to Boolean, it becomes true. This is because 1 is considered a "truthy" value in JavaScript's type conversion system.
Understanding Truthy and Falsy Values
JavaScript has specific rules for Boolean conversion. The number 1 (and all non-zero numbers) are truthy values, meaning they convert to true.
Example: Converting 1 to Boolean
<!DOCTYPE html>
<html>
<body>
<p>Convert 1 to Boolean</p>
<script>
var myVal = 1;
document.write("Boolean: " + Boolean(myVal));
</script>
</body>
</html>
Boolean: true
Different Ways to Convert
There are multiple ways to convert 1 to Boolean in JavaScript:
<!DOCTYPE html>
<html>
<body>
<script>
// Method 1: Boolean() function
console.log(Boolean(1));
// Method 2: Double NOT operator
console.log(!!1);
// Method 3: Implicit conversion in if statement
if (1) {
console.log("1 is truthy");
}
</script>
</body>
</html>
true true 1 is truthy
Comparison with Other Numbers
| Number | Boolean Conversion | Reason |
|---|---|---|
| 1 | true | Non-zero number |
| 0 | false | Zero is falsy |
| -1 | true | Non-zero number |
| 42 | true | Non-zero number |
Conclusion
When 1 is converted to Boolean in JavaScript, it becomes true because it's a non-zero number. All non-zero numbers are truthy values in JavaScript's type conversion system.
Advertisements
