JavaScript Variables and Data Types
- Variables are used to store data in JavaScript.
- Each variable holds a specific value.
- JavaScript automatically identifies data types.
- Common data types include String, Number, Boolean, Object, Null, Undefined, Symbol, and BigInt.
- Variables help write reusable and maintainable code.
- Data types determine how values behave during execution.
What are JavaScript Variables?
JavaScript variables are named containers used to store information. A variable can contain text, numbers, boolean values, objects, arrays, or other data types. Instead of writing the same value repeatedly, developers store it in a variable and reuse it whenever required.
Example of JavaScript Variables
let name = "John";
let age = 22;
console.log(name);
console.log(age);
Why Use Variables?
Store Data
Variables keep values for later use.
Reusable
Write once and use multiple times.
Readable
Meaningful variable names improve code readability.
Flexible
Values can be updated whenever required.
JavaScript Data Types
A data type tells JavaScript what kind of value is stored inside a variable. JavaScript is dynamically typed, meaning you don't need to specify the type manually.
- String
- Number
- Boolean
- Undefined
- Null
- Object
- Symbol
- BigInt
Example of Data Types
let city = "Lucknow";
let marks = 95;
let passed = true;
console.log(typeof city);
console.log(typeof marks);
console.log(typeof passed);
JavaScript Global Variables
A global variable is declared outside any function and can be accessed from any part of the program. They are useful for shared data but should be used carefully to avoid unexpected changes.