Welcome to Chapter 4: Variables in JavaScript. In this chapter, we will explore variables, their types, and how to use them efficiently in JavaScript.
By the end of this chapter, you will:
- Understand what variables are.
- Learn the difference between
var
,let
, andconst
. - Know how to declare and initialize variables.
- Follow best practices for using variables.
A variable is a container that holds data. It allows us to store, update, and retrieve values in our programs.
JavaScript provides three ways to declare variables:
var
(Old way, not recommended)let
(Preferred for variables that can change)const
(Preferred for values that do not change)
var name = "John"; // Using var (not recommended)
let age = 25; // Using let
const country = "USA"; // Using const
Feature | var |
let |
const |
---|---|---|---|
Re-declaration | Allowed | Not allowed | Not allowed |
Re-assignment | Allowed | Allowed | Not allowed |
Scope | Function | Block | Block |
Hoisting | Yes | No | No |
if (true) {
let x = 10;
const y = 20;
}
console.log(x); // Error: x is not defined
console.log(y); // Error: y is not defined
console.log(myVar); // Undefined
var myVar = "Hello";
- Use
const
by default, and uselet
only if the value needs to change. - Avoid
var
because it has function scope and can cause unexpected issues. - Use meaningful names for variables to improve code readability.
- Follow camelCase naming convention, e.g.,
userName
,totalAmount
. - Initialize variables properly to avoid
undefined
values.
- Declare three variables using
var
,let
, andconst
and log them to the console. - Write a program where a variable declared with
let
is updated later. - Try to reassign a
const
variable and note the error. - Create a function that demonstrates block scope with
let
andconst
. - Explain hoisting by writing a program that uses
var
before declaration.
- Variables store data and help in managing values dynamically.
- JavaScript provides
var
,let
, andconst
for variable declaration. let
andconst
are block-scoped, whereasvar
is function-scoped.- Always prefer
const
for constants andlet
for changeable values.
This chapter is presented by Muhammad Raza (SMRIT - Smart Modern Revolutionary IT Training). 🚀
متغیرات (variables) ایسے کنٹینرز ہوتے ہیں جو ڈیٹا ذخیرہ کرنے کے لیے استعمال کیے جاتے ہیں۔
جاوا اسکرپٹ میں تین طریقے ہیں:
var
(پرانا طریقہ، تجویز نہیں کیا جاتا)let
(تبدیل ہونے والے متغیرات کے لیے بہترین)const
(مستقل متغیرات کے لیے)
var name = "Ali"; // var کا استعمال (تجویز نہیں کیا جاتا)
let age = 30; // let کا استعمال
const country = "Pakistan"; // const کا استعمال
خصوصیت | var |
let |
const |
---|---|---|---|
دوبارہ اعلان | ممکن | ممکن نہیں | ممکن نہیں |
دوبارہ تفویض | ممکن | ممکن | ممکن نہیں |
دائرہ کار | فنکشن | بلاک | بلاک |
ہوائسٹنگ | ہاں | نہیں | نہیں |
if (true) {
let x = 10;
const y = 20;
}
console.log(x); // غلطی: x متعین نہیں ہوا
console.log(y); // غلطی: y متعین نہیں ہوا
console.log(myVar); // غیر متعین
var myVar = "Hello";
- ڈیفالٹ طور پر
const
استعمال کریں،let
صرف تب استعمال کریں جب ویلیو تبدیل کرنی ہو۔ var
سے پرہیز کریں کیونکہ یہ فنکشن اسکوپ رکھتا ہے۔- مناسب اور وضاحتی نام استعمال کریں تاکہ کوڈ پڑھنے میں آسانی ہو۔
- camelCase کا استعمال کریں جیسے
userName
,totalAmount
۔ - متغیرات کو فوری متعین کریں تاکہ
undefined
کی غلطیوں سے بچا جا سکے۔
var
,let
, اورconst
سے متغیرات بنا کر کنسول میں پرنٹ کریں۔let
سے ایک متغیر بنا کر بعد میں اسے اپڈیٹ کریں۔const
والے متغیر کو ری-اسائن کر کے ایرر دیکھیں۔let
اورconst
کے بلاک اسکوپ کو ایک فنکشن کے ذریعے سمجھائیں۔var
کے ساتھ ہوائسٹنگ کا ایک پروگرام لکھ کر فرق واضح کریں۔
- متغیرات ڈیٹا کو ذخیرہ اور مینیج کرنے کے لیے استعمال ہوتے ہیں۔
var
,let
, اورconst
کا مختلف دائرہ کار ہوتا ہے۔let
اورconst
بلاک اسکوپ رکھتے ہیں، جبکہvar
فنکشن اسکوپ رکھتا ہے۔- بہتر کوڈنگ کے لیے
const
کو ترجیح دیں۔
یہ باب محمد رضا (SMRIT - Smart Modern Revolutionary IT Training) کی جانب سے پیش کیا گیا ہے۔ 🚀