How to declare variables in different ways in JavaScript? Last Updated : 06 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, variables can be declared using keywords like var, let, or const, each keyword is used in some specific conditions. Understanding these declarations is crucial for managing variable lifetimes and avoiding unintended side effects in code.Table of Content JavaScript varJavaScript letJavaScript const Difference Between var, let, and constJavaScript varThis keyword is used to declare variables globally. If you use this keyword to declare a variable then the variable can be accessible globally and changeable also. It is good for a short length of codes, if the codes get huge then you will get confused.Syntax:var variableName = "Variable-Value;"Example: This example shows the use of var. javascript var geeks = "GeeksforGeeks"; console.log(geeks); OutputGeeksforGeeks JavaScript letThis keyword is used to declare variables locally. If you use this keyword to declare a variable then the variable can be accessible locally and it is changeable as well. It is good if the code gets huge.Syntax:let variableName = "Variable-Value;"Example: This example shows the use of let. javascript if (true) { let geeks = "GeeksforGeeks"; console.log(geeks); } /* This will be error and show geeks is not defined */ console.log(geeks); OutputGeeksforGeeksJavaScript constThis keyword is used to declare variable locally. If you use this keyword to declare a variable then the variable will only be accessible within that block similar to the variable defined by using let and difference between let and const is that the variables declared using const values can't be reassigned. So we should assign the value while declaring the variable.Syntax:const variableName = "Variable-Value;"Example: This example shows the use of const. javascript const geeks = "GeeksforGeeks"; console.log(geeks); OutputGeeksforGeeks Difference Between var, let, and constJavaScript varJavaScript letJavaScript constCan be redeclaredCannot be redeclaredCannot be redeclaredCan be reassigned a valueCan be reassigned a valueCannot reassign the valueOnly have global and function scopeCan have a block scopeCan have a block scopeVariables are hoisted on top and can be used anywhereVariables must be initialized before useVariables must be initialized before useCan be redeclared anywhere in the programCannot be redeclared inside a blockCannot be redeclared inside a block Comment More infoAdvertise with us Next Article Difference Between Variables and Objects in JavaScript S skyridetim Follow Improve Article Tags : JavaScript Web Technologies ES6 Similar Reads How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab 2 min read How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla 2 min read How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var 2 min read How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth 2 min read Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It 2 min read How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read Like