Lesson 8
Data Types
Data types specify the kind of value a variable can hold in JavaScript. They help the engine understand how to store and work with the data.
- Primitive Data Types
- Reference (Non-Primitive) Types
- typeof Operator
- Type Conversion
Primitive types store a single value.
String
"abc"
Sequence of characters.
let name = "Vivek";Number
123
Integers and floating point numbers.
let age = 29;Boolean
true
Logical values: true or false.
let isActive = true;Undefined
undefined
Variable declared but not assigned a value.
let x;Null
null
Intentional absence of any value.
let user = null;Symbol
Φ
Unique and immutable value (ES6).
let id = Symbol('id');These types can store collections of values.
Object
{ }
Collection of key-value pairs.
let person = { name: 'Vivek' };Array
[ ]
Ordered list of values.
let numbers = [1, 2, 3];Function
f()
Reusable block of code.
function greet() { }Used to check the type of a value.
1typeof "Vivek"; // "string"2typeof 29; // "number"3typeof true; // "boolean"4typeof undefined; // "undefined"5typeof null; // "object" (JS quirk)6typeof {}; // "object"7typeof []; // "object"8typeof function(){}; // "function"Implicit (automatic)
"5" + 2; // "52" number becomes string"5" - 2; // 3 string becomes numbertrue + 1; // 2 true becomes 1Explicit (you convert)
Number("5"); // 5String(29); // "29"Boolean(0); // falseRun the code to explore data types.
JavaScript
Output
Your output will appear here...
Remember:
- null is a special case in JavaScript.
- Arrays and functions are objects.
- Use typeof to debug and understand your variables.
