Value Is Boolean

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will dive into the concept of boolean values in JavaScript. We will explore how to check whether a given value is a boolean primitive or not using the typeof operator. By the end of this lab, you will have a solid understanding of how to identify boolean values in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28412{{"`Value Is Boolean`"}} javascript/data_types -.-> lab-28412{{"`Value Is Boolean`"}} javascript/arith_ops -.-> lab-28412{{"`Value Is Boolean`"}} javascript/comp_ops -.-> lab-28412{{"`Value Is Boolean`"}} end

Checking if a Value is Boolean

To check if a value is a boolean primitive in JavaScript, use the typeof operator with the === comparison operator.

const isBoolean = (val) => typeof val === "boolean";

Here's an example of how to use isBoolean() function to check if a value is boolean:

isBoolean(null); // returns false
isBoolean(false); // returns true

To start practicing coding, open the Terminal/SSH and type node.

Summary

Congratulations! You have completed the Value Is Boolean lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like