var let const diffrence
- Get link
- X
- Other Apps
JavaScript में var, let, और const तीनों का उपयोग वेरिएबल्स को डिक्लेयर करने के लिए किया जाता है, लेकिन इनकी उपयोगिता और व्यवहार में कुछ महत्वपूर्ण अंतर होते हैं। आइए इन तीनों के बीच के अंतर को समझते हैं:
1. var
Scope (स्कोप):
varके साथ डिक्लेयर किया गया वेरिएबल फंक्शन-स्कोप होता है। इसका मतलब है कि यह वेरिएबल पूरे फंक्शन में कहीं भी एक्सेस किया जा सकता है, जिसमें यह डिक्लेयर हुआ है।Hoisting (होइस्टींग):
varहोइस्टीड होता है, यानी कि डिक्लेरेशन को ऑटोमैटिकली फंक्शन के टॉप पर ले जाया जाता है।Re-declaration (री-डिक्लेरेशन):
varके साथ एक ही वेरिएबल को फिर से डिक्लेयर किया जा सकता है।उदाहरण:
javascriptvar name = "Rahul"; var name = "Rohit"; // वैलिड
2. let
Scope (स्कोप):
letका उपयोग ब्लॉक-स्कोप (जैसे कि{}के अंदर) वेरिएबल डिक्लेरेशन के लिए किया जाता है। इसका मतलब है कि यह वेरिएबल केवल उस ब्लॉक के अंदर ही एक्सेस किया जा सकता है जिसमें यह डिक्लेयर हुआ है।Hoisting (होइस्टींग):
letभी होइस्टीड होता है, लेकिन इसमें टेम्पोरल डेड ज़ोन (TDZ) होती है, जिससे वेरिएबल को डिक्लेरेशन से पहले एक्सेस करना संभव नहीं होता।Re-declaration (री-डिक्लेरेशन):
letके साथ एक ही वेरिएबल को फिर से डिक्लेयर नहीं किया जा सकता है।उदाहरण:
javascriptlet name = "Rahul"; let name = "Rohit"; // एरर होगी
3. const
Scope (स्कोप):
constभी ब्लॉक-स्कोप वेरिएबल डिक्लेरेशन के लिए उपयोग किया जाता है।Hoisting (होइस्टींग):
constभी होइस्टीड होता है, और इसमें भी टेम्पोरल डेड ज़ोन (TDZ) होती है।Re-declaration (री-डिक्लेरेशन) और Re-assignment (री-असाइनमेंट):
constके साथ डिक्लेयर किया गया वेरिएबल न तो री-डिक्लेयर किया जा सकता है और न ही री-असाइन। मतलब, एक बार वैल्यू असाइन करने के बाद इसे बदला नहीं जा सकता।उदाहरण:
javascriptconst name = "Rahul"; name = "Rohit"; // एरर होगी
निष्कर्ष:
varका उपयोग अब बहुत कम किया जाता है क्योंकि यह कुछ स्थितियों में अनपेक्षित व्यवहार कर सकता है।letका उपयोग तब करना चाहिए जब आपको वेरिएबल को बाद में री-असाइन करने की आवश्यकता हो।constका उपयोग तब करना चाहिए जब आप सुनिश्चित हों कि वेरिएबल की वैल्यू कभी नहीं बदलेगी।
In JavaScript, var, let, and const are used to declare variables, but they differ in scope, hoisting, and reassignment. Here’s a breakdown of the differences:
1. var
Scope: Variables declared with
varare function-scoped. This means they are accessible throughout the entire function where they are declared.Hoisting:
varvariables are hoisted, meaning the declaration is moved to the top of the function. However, the initialization stays in place.Re-declaration: You can re-declare a variable using
varwithout any error.Example:
javascriptvar name = "Rahul"; var name = "Rohit"; // This is valid
2. let
Scope:
letis block-scoped, which means the variable is only accessible within the block{}where it is declared.Hoisting:
letis also hoisted, but it has a Temporal Dead Zone (TDZ) that prevents accessing the variable before the declaration.Re-declaration: You cannot re-declare a variable using
letwithin the same scope.Example:
javascriptlet name = "Rahul"; let name = "Rohit"; // This will cause an error
3. const
Scope: Like
let,constis block-scoped.Hoisting:
constis hoisted with a Temporal Dead Zone (TDZ), similar tolet.Re-declaration and Re-assignment: Variables declared with
constcannot be re-declared or re-assigned. Once a value is assigned, it cannot be changed.Example:
javascriptconst name = "Rahul"; name = "Rohit"; // This will cause an error
Summary:
varis function-scoped and allows re-declaration but is less commonly used due to potential unexpected behaviors.letis block-scoped, allows reassignment but not re-declaration within the same scope.constis also block-scoped, but it neither allows re-declaration nor reassignment, making it ideal for variables that should remain constant.
- Get link
- X
- Other Apps