GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript Boolean - GeeksforGeeks JavaScript BooleanLast Updated : 11 Jul, 2025 To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy nature. The Boolean() FunctionThe Boolean() function is used to explicitly convert a value to its Boolean equivalent. Truthy values return true, while falsy values return false.
Output true false Everything With a "Value" is TrueIn JavaScript, all objects, arrays, and non-empty strings are considered truthy values.
Output true true true Objects and arrays always evaluate to true in Boolean contexts. Everything Without a "Value" is FalseCertain values, like 0, null, undefined, NaN, and empty strings, are falsy.
Output true false These values lack meaningful content and thus evaluate to false. JavaScript Booleans as ObjectsWhile JavaScript supports primitive Booleans, the Boolean constructor can create Boolean objects. However, these objects are truthy regardless of their value.
Output [Boolean: false] object true Using new Boolean() is generally discouraged because it can lead to confusion. Boolean PrimitivesA Boolean primitive is a simple representation of true or false. This is the most commonly used Boolean type in JavaScript.
Output boolean Boolean primitives are lightweight and efficient for logical operations. Boolean CoercionJavaScript implicitly converts values to Boolean in contexts like conditions and loops.
Output Invalid input Empty strings evaluate to false, so "Invalid input" is logged. Constructor and InstanceThe Boolean constructor can create Boolean objects, but primitive Booleans are preferred.
Output boolean object Primitive Booleans are easier to use and avoid unnecessary complexity. Truthy and Falsy ValuesFalsy Values: Values that are evaluated as false when used in a Boolean. Unlike truthy values, falsy values represent "nothingness," "emptiness," or "failure."
Truthy Values: Values that are evaluated to be true when used in a Boolean context, such as in conditional statements or logical operations.
Truthy/Falsy in Action
Output false true false true
Boolean Coercion in Conditions
Output Age missing Since userAge is 0, it evaluates to falsy, and "Age missing" is logged. Practical Applications1. Conditional Statements
Output Pass 2. Ternary Operator
Output false 3. Logical Short-Circuiting
Output Guest We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |