All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Declare A Variable In JavaScript

Last Updated : Mar 11, 2024

How To Declare A Variable In JavaScript

In this tutorial we will show you the solution of how to declare a variable in JavaScript, as we know variables are containers that store values in itself and in general, it is said that variables are containers for storing data.

So, in this article we are going to understand how to declare variable and store data in them. Let us start our article.

Step By Step Guide On How To Declare A Variable In JavaScript :-

As, there are many ways with the help of which we are able to declare variables in JavaScript.

But basically we use three different ways that are let, const and var. All of them are for their different purposes. Let us understand them one by one with the help of example.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
</head>
<body>
<script>
// declaration of variable with help of var
 var name = "robert downey";
 console.log(name);
// declaration of variable with help of let
 if (true) {
 let age = "25";
 console.log(age);
 }
// declaration of variable with help of const
 console.log(talkers);
 const code = "talkers code";
 console.log(code);
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body we create our script tag which is a paired tag and we write JavaScript inside this tag.
  6. Here, we describe three methods to declare variables in JavaScript. First method is with help of let, second one is with help of var and at last we use const to declare variable.
  7. All the methods are same and use the same way to declare variable. You can use any one of the method to declare variable. When you run or open this file on browser, you can see nothing because we print our outputs in console.
  8. To see outputs, we have to inspect our webpage by right click of mouse and by typing ctrl+shift+i. And when you inspect this and open console tab, then we able to see outputs under this option. We hope you help codes.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to create an element with class using JavaScript. I hope this tutorial on how to declare a variable in JavaScript helps you.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪