Understanding JavaScript Variables Types or JavaScript Data Types, is essential for anyone starting their journey with JavaScript. As one of the most versatile programming languages, JavaScriptโs variables can hold different kinds of values.
In this part of JavaScript Free Series of LogicalDuniya, we will explore JavaScript Data Types and learn how they work, solving different doubts like – JavaScript Variables Types ?
What Are Data Types in JavaScript?
In simplest words – JavaScript data types define the kind of value a variable can hold. For example, it manages whether the variable will hold number
type age of students, or String
type name of students.
JavaScript is a dynamically typed language, which means you donโt have to specify the type of a variable when declaring it. The language determines the type of the variable based on the value assigned to it.
Hereโs a quick example:
JavaScript has two categories of data types:
- Primitive Data Types
- Non-Primitive Data Types (also called – Objects)
1. Primitive Data Types
These include simple and immutable values:
- String: Represents textual data.
- Number: Holds numerical values.
- Boolean: Represents logical values
true
orfalse
. - Undefined: A variable declared but not assigned a value.
- Null: Represents a deliberate non-value.
- BigInt: Used for large integers.
- Symbol: Represents unique identifiers.
Example :
let str = "Hi, LogicalDuniya !"; // String
let num = 42; // Number
let isActive = false; // Boolean
let unknown; // Undefined
let empty = null; // Null
let largeNumber = 12345678901234567890n; // BigInt
let sym = Symbol('unique'); // Symbol
2. Non-Primitive Data Type :
Object: Represents collections of key-value pairs.
Example :
- Arrays
- functions
- dates, etc.
What Are JavaScript Variables?
Variables are like containers, which we use for storing data values. In JavaScript, you declare variables using the keywords var
, let
, or const
. While declaring variables, it’s essential to understand JavaScript Variable Types as they determine the type of data a variable can hold.
var age = 25; // Number
let name = "John"; // String
const STATUS = false; // Boolean
Working with JavaScript Variables Types
When using JavaScript variables types, understanding Type Coercion and how JavaScript interprets data types dynamically is vital.
Example:
let value = "100"; // String
value = value * 2; // JavaScript converts it to a number
console.log(value); // Output: 200
Checking JavaScript Variable Types
In JavaScript, we can also check, which type of data a variable is currently holding. To check the type of a variable, you use the typeof
operator.
Example :
let name = "LogicalDuniya.com";
console.log(typeof name); // Output: string
let isAvailable = true;
console.log(typeof isAvailable); // Output: boolean
Declaring Variables in JavaScript
JavaScript offers three ways to declare variables. All have different properties and use case. Here are those -:
- var: Function-scoped, can be re-declared and updated.
- let: Block-scoped, can be updated but not re-declared.
- const: Block-scoped, cannot be updated or re-declared.
Example:
var x = 10; // Can be re-declared
let y = 20; // Block-scoped
const z = 30; // Immutable
Advanced Topics: Dynamic Typing
JavaScript allows reassignment of variables with different types, showcasing its dynamic nature. Let’s understand this with an example.
Example:
let value = 5; // Number
value = "Now I'm a string"; // String
console.log(value); // Output: Now I'm a string
How to Use JavaScript Variables Types in Real-World Scenarios
Scenario 1: Using null
and undefined
to check API responses:
let data = fetchData() || null;
if (data === null) {
console.log("No data available in LogicalDuniya.com");
}
Scenario 2: Using typeof
to validate user input:
let age = prompt("Enter your age:");
if (typeof +age === "number" && !isNaN(age)) {
console.log("Valid number!");
} else {
console.log("Please enter a valid number");
}
What we learn today ?
We learnt about JavaScript variable declarations and JavaScript data types. We have also seen numerous example of JavaScript primitive types as well as non-primitive types.
- To learn more simple and detailed JavaScript guides, do check this link.
- To use hundreds of free tools to fasten your daily tasks, visit our – Tools.LogicalDuniya.com page.
- To see tutorials of other technologies & concepts, visit our Tutorials page.
Happy Learning, Cheers.
Shubham : )