JavaScript Variables Types – Syntax with Examples

Last updated on : Jan 17, 2025 by Shubham Srivastava

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 ?

JavaScript Variables Types - LogicalDuniya_com

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:

  1. Primitive Data Types
  2. 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 or false.
  • 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 -:

  1. var: Function-scoped, can be re-declared and updated.
  2. let: Block-scoped, can be updated but not re-declared.
  3. 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.

Happy Learning, Cheers.
Shubham : )

Shubham

Shubham Srivastava

Hey there, my name is Shubham and I'm working as an SDET at a US based FinTech company. I've more than 7 years of working experience and using my knowledge - I want make this platform super helpful for those, who want to solve any software development and testing related problems. Comment your query / request or suggestion in the below section, and I'll try to respond within 24 hours. Keep improving, Keep coding !!

Recommended Posts :


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close