How to use Let and Const in JavaScript ?

In this tutorial we’re going to learn how to use Const and let function in JavaScript. JavaScript let is used to declare variables. The variables declared using let are block-scoped. This means they are only accessible within a particular block. For example,

Let keyword

<script type="text/javascript">
        let name = 'Amit';
        {
            let name = 'Kumar';
            console.log(name);
        }
        console.log(name);
    </script>

Output :-

Const example:-

We use the const keyword in JavaScript to declare variables whose value can be initialized only at the time of declaration. It is similar functionality of declaring variables as the other keywords provided in JavaScript i.e. var and let

Example:-

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>

<body>
    <div id="result"></div>
    <script type="text/javascript">
      const NUM = 10;
      var text = "";
      for (let NUM = 0; NUM < 10; NUM++) {
         text += NUM + ",";
      }
      text += "<br>"
      try {
         NUM = 20;
      } catch (err) {
         text += err.message;
      }
      document.getElementById("result").innerHTML = text;
    </script>

</body>

</html>

Output:-

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

How to print Null value Using JavaScript ?

In JavaScript, if you want to print the string “null” when a variable is null, you can do so add in !== operator. Benefits of Using if-else…

How to use Prompt function in JavaScript ?

In this tutorial we’re going to learn how to use Prompt function using JavaScript. Just use below code and put put inside the script tag Output:- I…

JavaScript Comparison and Logical Operators

In this tutorial I’m going to learn how to use Comparison and logical operators in laravel. Comparison operators compare two values and give back a boolean value:…

How to use JavaScript Constructor Function ?

A JavaScript constructor function is a function that is used to create new objects. It is called a constructor because it constructs new objects. To create a…

Top 10 IT Certification in The World ?

In this tutorial I’m going to share top 10 IT Certification in the world who help to grow your carrier in fast way also we ‘having covering…

Phantom installation failed TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string.

Error:- Solutions:- Run this Output :-

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x