JavaScript Variables In Hindi

📔 : Java Script 🔗

Variables किसी memory को दिया गया नाम है  जो कि किसी value  को Hold करती है या store करती है।  Means जब हम Variable define करते हैं  तो हम memory में value को store करके उसे नाम दे रहे होते हैं।  हम कह सकते हैं कि variables 'named storage' होते हैं जिनका use किसी value को store करने के लिए किया जाता है।

JavaScript Declare Variables

JavaScript में variables दो तरह से declare किये जाते हैं -

  1. using let keyword
  2. using var keyword

var name="Rahul Kumar Rajput";
let age = 23;
var address = "454t Jaipur Rajsthan";

दोनों में से (let और var) किसी भी keyword का use हम variables को declare करने के लिए कर सकते हैं।


JavaScript में Variables Define करते समय हमें कोई type करने की जरूरत भी नहीं पड़ती है , जैसे कि JAVA, C में कोई variable define करते समय इसका Type define करना पड़ता है। लेकिन JavaScript में आप जिस Type की value को assign कराओगे variable उसी type की value को hold कर लेगा।

JavaScript Variable Example

Another File : variable.html

Copy Fullscreen Close Fullscreen Run
!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Variables</title>
  </head>
  <body>
    <script>
      let str = 'String Value';
      let num = 78;
      let num2 = 78.89;
      let bool = true;
      document.write(typeof str+'<br>');
      document.write(typeof num+'<br>');
      document.write(typeof num2+'<br>');
      document.write(typeof bool+'<br>');
    </script>
  </body>
</html>
Output
string
number
number
boolean

Note - typeof  एक reserve keyword है जिसका use variable type जानने के लिए किया जाता है। और <br> का use line break करने के लिए किया जाता है।

Difference Between var And let

JavaScript var और let मैं difference यह है कि var keyword के through define किये गए variables global scope होता है जिन्हे हम window object के through access कर सकते हैं।
जबकि let keyword के through define किये गए variables का scope limited होता है।


इसक अलावा var keyword के through define किये गए variables को आप re-declare कर सकते हैं जबकि let keyword के through define किये गए variables को आप re-declare नहीं कर सकते।

Another File : variable2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Variables</title>
  </head>
  <body>
    <script>
      var x= 10;
      let y=20;
      document.write(window.x); /* fine No problem*/
      document.write(window.y); /* undefined */

      /*now re-declare them*/
      var x= 10;
      let y=20;
    </script>
  </body>
</html>
Output
Uncaught SyntaxError: redeclaration of let y
note: Previously declared at line 10, column 7

Variable Naming Rules

  1. JavaScript में variables के name reserve keywords नहीं होने चाहिए।
  2. किसी भी variables का नाम number से start नहीं करना चाहिए ये allowed नहीं हैं।
  3. किसी भी variables का नाम underscore से नहीं करना चाहिए , underscore का use JavaScript में कुछ specific things define की गयी हैं।
  4. Variables का नाम human readable और meaningful होने चाहिए जिससे confusion न हो।

JavaScript Constant Variables

Constants, Identifier होते हैं जिन्हे हम variables की तरह ही define करते हैं , हालाँकि variables की तरह इनकी value mutable नहीं होती , means हम script के execution के समय इनकी value को change या reassign नहीं कर सकते हैं।

const my_var = 'Some Value';

जैसे हम variable को Define करके हम Runtime पर भी increment / decrement या new value assign कर देतें हैं। और अगर हम reassign करते हैं तो error आएगी।


JavaScript में Constants Variable define करने के लिए const keyword का use करते हैं।

See Example :

Another File : constant_var.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Variables</title>
  </head>
  <body>
    <script>
      const x = 'Some Value';
      /* try to reassign with new value */
      x = 'New Value';
    </script>
  </body>
</html>
Output
Uncaught TypeError: invalid assignment to const 'x'

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers