
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Various ways to define a variable in JavaScript
There are three ways to define a variable in JavaScript −
- let − The JavaScript Let keyword introduced in 2015 allows us to define block scoped variables. Variables declared using let are not hoisted.
- var − The JavaScript var keyword is used for creating function scoped variables and are hoisted.
- const − The const declarations create variables that cannot be reassigned to some other value or redeclared later. It was introduced in ES2015. The variables declared using const are not hoisted.
Following is the code to define variables in JavaScript with let, var and const −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>Define variable in JavaScript</h1> <div class="result"></div> <button class="Btn">Click here</button> <h3> Click on the above button to declare variables using let, const and var and access them; </h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); BtnEle.addEventListener("click", () => { resEle.innerHTML = "a = " + a + "<br>"; try { resEle.innerHTML += "b = " + b; } catch (err) { resEle.innerHTML += err + "<br>"; } try { resEle.innerHTML += "c = " + c; } catch (err) { resEle.innerHTML += err + "<br>"; } var a = 22; let b = 44; const c = 99; try { c = 44; } catch (err) { resEle.innerHTML += err + "<br>"; } }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to define global variable in a JavaScript function?
- Define a variable?
- How to define a variable in C++?
- Must you define a data type when declaring a variable in JavaScript?
- What are various ways to compare dates in Java?
- What are various ways to compare time values in Java?
- Explain various ways to unwrap an optional in the Swift language
- How many ways are there to initialize a final variable in java?
- Ways to create a Set in JavaScript?
- How to define a method in JavaScript?
- How to define a function in JavaScript?
- Ways to get to a specific sum in JavaScript
- How do we use enum keyword to define a variable type in C#?
- How to unset a JavaScript variable?
- Different ways to declare variable as constant in C and C++

Advertisements