How to Declare a Boolean Variable in JavaScript
- Variable Stores Values in JavaScript
- Variable Declaration in JavaScript
- Use Boolean Data Type to Declare Boolean Values or Logical Values in JavaScript
- Declaring Boolean Datatype Variable in JavaScript
In this article, we will be explaining how we can declare variables with different data types in JavaScript.
We define its data type, and the possible values to be entered are assigned to the variable so that they do not breach the data type requirements.
Variable Stores Values in JavaScript
A variable is a memory location where the program can store values and fetch them whenever required. Every variable must have a unique name.
The data stored in the variables can be changed throughout the program execution, meaning that the JavaScript variables are not mutable. The variable’s data type tells what type of data is stored.
Variable Declaration in JavaScript
When a memory location is assigned a specific name and a data type for storing values, it is a variable declaration.
However, it is called variable initialization when assigning values to the variable. The variable declaration and initialization can be done separately and collectively.
The variable can be reassigned some value during the program execution. The values can be taken as inputs from the user.
Use Boolean Data Type to Declare Boolean Values or Logical Values in JavaScript
The Boolean data type stores Boolean values or logical values as true
or false
.These are also called binary values as the computer only understands 0
and 1
.
We can store binary values in a variable. For that, we need a Boolean-type variable.
But in JavaScript, the two syntaxes for declaring a variable are using let
and var
keywords. The following are the syntaxes for declaring a variable in JavaScript using each keyword.
var number;
let std;
We created number
and std
variables variables using var
and std
keywords. We don’t need to specify the variable’s data type while declaring them.
The variable’s data type gets set by the type of value stored in it. We can see the following code segment for our better understanding.
var number = 10;
var numb = '10';
We created two variables, number
and numb
, with similar-looking values, but they are not the same.
There is a huge difference between the two variables. The first variable, number
, has a value of 10
, which is an integer value, and the second variable, numb
, has a value of "10"
.
Which shows it is a string variable. The keyword used for declaring both variables was the same.
No datatype was given, yet the values stored in the variables decided the variable’s datatype.
Declaring Boolean Datatype Variable in JavaScript
Declaring a Boolean datatype variable is also the same as declaring any other datatype variable. But it would help if you were careful while assigning the values.
One common mistake would be to put ""
around the values, thus making them string variables.
The other mistake would be to use 0
and 1
as values that are themselves integer values so that the variable will become an integer.
var bool1 = 1;
var bool2 = '1';
var bool3 = 'true';
var bool4 = true;
The first variable, bool1
, was assigned 1
, making it an integer variable. Variables 2 and 3 bool2
and bool3
show that the values were written in ""
quotation marks, making it a string variable.
But in the fourth variable, bool4
, the value was given with quotation marks, and the value was one of the two possible values of a Boolean (true or false). This variable is now a Boolean variable.
JavaScript doesn’t restrict its programmers to specify the variable’s datatype in advance so that no other value can be stored in the variable.
Still, it allows to create a variable and store whatever value they want to store in it, and then the variable gets its datatype specified by the type of value stored.
On the one hand, this allows the ease of using variables but makes it a little delicate while entering the variables’ values.