Welcome to Chapter 5: Data Types in JavaScript. In this chapter, we will explore different data types in JavaScript and understand how they work.
By the end of this chapter, you will:
- Understand what data types are.
- Learn the difference between primitive and non-primitive data types.
- Know how to use different data types effectively.
Data types define the type of value stored in a variable. JavaScript has two main categories of data types:
- Primitive Data Types (Stored directly in memory)
- Non-Primitive (Reference) Data Types (Stored by reference)
Primitive data types include:
String
(Text values)Number
(Numeric values)Boolean
(True/False values)Null
(Empty value)Undefined
(Variable declared but not assigned)Symbol
(Unique identifiers, ES6+)BigInt
(Large numbers, ES11+)
let name = "John"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let emptyValue = null; // Null
let notAssigned; // Undefined
let uniqueId = Symbol("id"); // Symbol
let bigNumber = 123456789012345678901234567890n; // BigInt
These include:
Object
(Collection of key-value pairs)Array
(List of values)Function
(Reusable block of code)
let person = { name: "John", age: 25 }; // Object
let numbers = [1, 2, 3, 4, 5]; // Array
let greet = function() { console.log("Hello, World!"); }; // Function
Feature | Primitive | Non-Primitive |
---|---|---|
Stored in memory | Directly | By reference |
Mutability | Immutable | Mutable |
Example | String , Number |
Object , Array |
- Use
const
for constants andlet
for variables that change. - Prefer
null
overundefined
when explicitly assigning an empty value. - Use objects for complex data instead of multiple separate variables.
- Use arrays for collections of values.
- Check data type using
typeof
.
console.log(typeof "Hello"); // String
console.log(typeof 100); // Number
console.log(typeof true); // Boolean
console.log(typeof {}); // Object
console.log(typeof []); // Object (Array is a type of object)
console.log(typeof function(){}); // Function
- Declare a variable of each primitive data type and log its value.
- Create an object with at least three properties.
- Write a function and store it in a variable.
- Declare an array and access its elements using indexes.
- Use
typeof
to check the data type of different variables.
- JavaScript has primitive and non-primitive data types.
- Primitive types include
String
,Number
,Boolean
,Null
,Undefined
,Symbol
, andBigInt
. - Non-primitive types include
Object
,Array
, andFunction
. - Primitive values are stored directly, while non-primitive values are stored by reference.
This chapter is presented by Muhammad Raza (SMRIT - Smart Modern Revolutionary IT Training). 🚀
ڈیٹا ٹائپس وہ اقسام ہوتی ہیں جو کسی بھی ویری ایبل میں محفوظ ہونے والی ویلیو کی نوعیت کو ظاہر کرتی ہیں۔
یہ وہ ڈیٹا ٹائپس ہیں جو براہ راست میموری میں محفوظ ہوتی ہیں:
- String (متنی اقدار)
- Number (عددی اقدار)
- Boolean (True/False)
- Null (خالی ویلیو)
- Undefined (متغیر متعین لیکن غیر مختص شدہ)
- Symbol (منفرد شناخت کار)
- BigInt (بہت بڑی عددی ویلیو)
let naam = "Ali"; // String
let umar = 30; // Number
let talib = false; // Boolean
let khaali = null; // Null
let mutaiyan; // Undefined
let pehchan = Symbol("id"); // Symbol
let baraAdad = 987654321012345678901234567890n; // BigInt
یہ ریفرنس کے ذریعے محفوظ ہوتے ہیں:
- Object (کلیدی جوڑوں کی کلیکشن)
- Array (ویلیوز کی فہرست)
- Function (قابل استعمال کوڈ بلاک)
let shakhs = { naam: "Ali", umar: 30 }; // Object
let adad = [1, 2, 3, 4, 5]; // Array
let salam = function() { console.log("سلام دنیا!"); }; // Function
const
مستقل اقدار کے لیے استعمال کریں،let
قابل تبدیلی متغیرات کے لیے۔null
کوundefined
پر ترجیح دیں جب کوئی خالی قدر متعین کرنی ہو۔- معقد ڈیٹا کے لیے objects استعمال کریں۔
- مجموعی ڈیٹا کے لیے arrays استعمال کریں۔
typeof
کا استعمال کرکے ڈیٹا کی قسم چیک کریں۔
console.log(typeof "سلام"); // String
console.log(typeof 50); // Number
console.log(typeof true); // Boolean
console.log(typeof {}); // Object
console.log(typeof []); // Object (Array بھی ایک object کی قسم ہے)
console.log(typeof function(){}); // Function
- ہر بنیادی ڈیٹا ٹائپ کے لیے ایک متغیر بنائیں اور اس کی ویلیو پرنٹ کریں۔
- تین پراپرٹیز کے ساتھ ایک object بنائیں۔
- ایک function لکھیں اور اسے ایک ویری ایبل میں محفوظ کریں۔
- ایک array بنائیں اور اس کے عناصر تک رسائی حاصل کریں۔
- مختلف متغیرات کی قسم
typeof
سے چیک کریں۔
- جاوا اسکرپٹ میں بنیادی اور غیر بنیادی ڈیٹا ٹائپس موجود ہیں۔
- بنیادی ٹائپس میں
String
,Number
,Boolean
,Null
,Undefined
,Symbol
, اورBigInt
شامل ہیں۔ - غیر بنیادی ٹائپس میں
Object
,Array
, اورFunction
شامل ہیں۔ - بنیادی اقدار براہ راست محفوظ ہوتی ہیں جبکہ غیر بنیادی اقدار ریفرنس کے ذریعے محفوظ ہوتی ہیں۔
یہ باب محمد رضا (SMRIT - Smart Modern Revolutionary IT Training) کی جانب سے پیش کیا گیا ہے۔ 🚀