

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Concept: Variable ko aap ek 'Khali Dibba' (Empty Box) samajh lein. Is dibbe ka kaam yeh hai ke yeh aap ka data apne andar sambhal kar rakhta hai taake aap use baad mein program mein kahin bhi istemal kar sakein. JavaScript mein variable banane ke liye hum buniyadi taur par do major keywords ka istemal karte hain: let aur const.
Farz karein ek game chal rahi hai aur user ka score abhi 0 hai. Baad mein jab woh behtar khelega to score badh kar 10 ho jata hai. Aisi situation mein hum let ka use karte hain kyunki is ka saman (value) badalna allowed hota hai.
let score = 0; // Dibbe ka naam 'score' hai aur andar '0' rakha hai. score = 10; // Ab hum ne score badal kar 10 kar diya. (This is completely Allowed)
Kuch cheezein pure program mein aisi hoti hain jo kabhi nahi badaltein, jaise aap ki date of birth ya pi (Ļ) ki fixed mathematical value. Inke liye hum const use karte hain taake koi galti se bhi value change na kar sake.
const pi = 3.14; // Yeh value ab completely freeze aur lock ho gayi hai. pi = 5; // Error: Assignment to constant variable! (Not Allowed)
Professional Rule of Thumb: Senior aur professional web developers ka ek sunehri asool hota hai: 'Hamesha const ka istemal karo, jab tak aap ko pakka pata na ho ke yeh value aage chal kar badle gi!' Is se bugs aane ke chances khatam ho jaate hain.
Category Real-World Application Scenario Code Reference Syntax
Settings Website configuration limits jo backend ya security ke liye fix rehti hain.
const maxLoginAttempts = 5;
API Link Website ka main link ya production server endpoint url.
const apiURL = "https://mywebsite.com";
Fixed Info User ka original mulk ya aisi unique ID jo badal nahi sakti.
const country = "Pakistan";
Ab hum ne data ko dibbe mein save to kar diya, lekin use screen par verify kaise karna hai? Us ke liye hum console.log() ka istemal karte hain. Yeh samajh lein ke yeh JavaScript ka ek 'Chashma' hai jo developers ko live environment mein dibbe ke andar ka asal saman aur value dikhata hai.
let studentName = "Ali"; // Screen/Console par output check karne ke liye: console.log (studentName); // Screen pe Output aayega: Ali