ES6 let and const variable declarations keywords in hindi

ES6 let and const keyword in hindi

Introduction

हेलो दोस्तों आज हम इस post में ES6 ke variable declarations let and const keyword के बारे में जानेगे!. let और const का इस्तेमाल कैसे  और किस level के scope में कर सकते है!. वह इस post में example के साथ समझेंगे!. let and const variable declarations keywords ES6 (ECMAScript 2015) में introduced हुवे थे!.

let declaration

let keyword का इस्तेमाल block-scoped variable को declared करने के लिए किया जाता है!. इसका मतलब है की let keyword के साथ declared variable केवल उस block में ही accessible हो सकता है जहा इसे defined किया गया है!.

Example:

<script> 
  function example() {
     let x = 10;
     if (true) {
    	let x = 20;
    	console.log(x); // Output: 20
     }
    console.log(x); // Output: 10
  }
</script>

ऊपर के example में देख सकते है हम अलग-अलग block में let keyword का इस्तेमाल करके variable “x” को दो variable declare किया है. first x variable को block के बहार declare किया है और उसका value 10 है जबकि दूसरा “x” variable को block के अंदर declared किया है और उसका value 20 है. जब हम inner block के अंदर “x” का value को print करते है तो यह 20 का output देता है, लेकिन जब हम inner block के बहार “x” के value को print करते है तो यह 10 को output करता है!.

const declaration

const keyword का इस्तेमाल उन variable को declared करने के लिए किया जाता है जिन्हे reassigned नहीं किया जा सकता है!. एक बार const keyword के साथ variable को value assign करने के बाद इसे chanaged नहीं किया जा सकता है!.

Example

<script>
  const PI = 3.14159;
  PI = 3.14; // Throws an error
  console.log(PI); // Output: 3.14159
</script>

ऊपर के example में हम देख सकते है हम constant PI declare किया है और इसमें 3.14159 value assign किया है,
जब हम PI में किसी दूसरी value को assign करने की कोसिस करते है तो वह एक error thrown करता है!. और जब हम PI का value को print करते है इसका output 3.14159 होता है। यह note करना important है की यदि const variable के value changed नहीं किया जा सकता है फिर भी value स्वयं से परिवर्तनशील हो सकता है!.

Example

<script>  
  const arr = [1, 2, 3];
  arr.push(4); // Valid operation
  console.log(arr); // Output: [1, 2, 3, 4]
</script>

ऊपर के example में देख सकते है constant array को declared किया है और array में 1,2 or 3 value assign है!. लेकिन इस array में new value 4 को push() method के मदद से add किया जा सकता है!.

1 thought on “ES6 let and const variable declarations keywords in hindi<span class="post-views-after-title"><span class="post-views-after-title"><div class="post-views content-post post- entry-meta"><span class="post-views-icon dashicons dashicons-visibility"></span><span class="post-views-label">Post Views : </span><span class="post-views-count">107</span></div></span></span>”

  1. Pingback: What is ES6 in hindi ? ES6 features introduction

Leave a Comment

Your email address will not be published. Required fields are marked *