Variables & Data types in JavaScript
A variable is a named storage location for holding data values. These values can be numbers, strings, objects, or other data types. Variables play a crucial role in programming, allowing developers to work with and manipulate data dynamically.
In JavaScript, data types refer to the kinds of values that variables can hold. JavaScript has two main categories of data types: primitive data types and Non primitive data types.
In the below PDF we discuss about Variables & Data Types in detail in simple language, Hope this will help in better understanding.
Declaring Variables:
Keywords To use a variable in JavaScript, you need to declare it using the var, let, or const keyword. Each keyword has its own scope and use cases:
- var: Declared variables with var have function-level scope, which means they are accessible within the entire function, but not outside it.
- let: Introduced in ECMAScript 6 (ES6), let allows block-level scope, making it a better choice for variables within a block, such as loops or conditional statements.
- const: Like let, const is also introduced in ES6 and provides block-level scope. The key difference is that once a value is assigned to a const variable, it cannot be reassigned.
Types of Data Types in JavaScript:
1. Primitive Data Types:
JavaScript has six primitive data types, which are the most basic building blocks of the language. They include:
- Number: Represents both integers and floating-point numbers.
- String: Represents sequences of characters enclosed in single or double quotes.
- Boolean: Represents a logical value – either true or false.
- Null: Represents the absence of a value or a non-existent object.
- Undefined: Represents an uninitialized or undeclared variable.
- Symbol: Introduced in ECMAScript 6, symbols are unique and immutable values often used as object keys.
2. Non-Primitive Data Types:
- Object: A collection of key-value pairs, where each key is a string and each value can be any data type, including other objects.
- Arrays: Although arrays in JavaScript are technically objects, they are worth mentioning separately due to their significance. Arrays are collections of values, and each value can be of any data type. They are declared using square brackets:
Related Question
A variable in JavaScript is a symbolic name for a value. It is a container that can hold different types of data, and its value can be changed during the execution of a program.
You can declare a variable in JavaScript using the var, let, or const keyword, followed by the variable name.
You can use the typeof operator to check the type of a variable.
Strings can be concatenated in JavaScript using the + operator.
Relevant
JavaScript JSON JSON stands for
JavaScript Function Generator Function generators,
JavaScript WeakSet A WeakSet is
JavaScript WeakMap A WeakMap is
JavaScript Proxy A JavaScript Proxy
JavaScript Boolean JavaScript Boolean is
JavaScript BigInt JavaScript BigInt represented