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 the below PDF we discuss about Variables & Data Types in detail in simple language, Hope this will help in better understanding.
- 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.
1. Primitive Data Types:
- 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.
You can use the typeof operator to check the type of a variable.