Variables in Javascript
Published by
sanya sanya
Variables in Javascript are the containers for storing the values of specific types. Variables can store a single value and there are three keywords in Javascript to store variables - Let, Var, and Const.
The code showing the declaration of variables in Javascript is mentioned below -
let message = "Hello World" console.log(message)
//Output - Hello World
The Variables can be understood by an example that in our kitchen, we have various food items such as pulses, sugar, tea, coffee, and so on. So, the food items are data values and the container are variables.
Scoping of Variables
Scoping refers to a particular region in the code where the assigned variable is accessible. It is simply the place where we can use our declared variable again.
However, there are two types of scopes -
Local Scope and Global Scope. The Local space is where the variables are accessed to a particular block of code Whereas Global variables can be accessed from anywhere in the code.
In the case of
Global Variable, the variables are like -
This Global scope can be understood by the example, for instance, If I am the owner of the website, I can access every piece of information whether it is data, content, etc. I will have no restrictions.
On the other hand, the visitor who comes to the website will have access to the information available publicly on the website and not of the backend.
Let, Var, and Const
All three keywords Let, Var, and const are used for declaring the variables. But a question arrives if all have the same working why not use only one?
So, the answer to this question is that the let keyword cannot be re-declared but can be updated, and also let is blocked scoped.
var keyword is globally scoped, can be re-declared, and updated within the scope.
Const Keyword is the most used keyword because the value initialized and declared with const cannot be changed throughout the code and is kept constant.
Variable Naming
There are certain limitations in giving names to the variables in Javascript which are mentioned below -
- The name of the variable must contain letters, digits, symbols, $, and _.
- The name of the variable must not start with a digit.
- The camel case conventions or Pascal conventions are commonly used in variable naming.
Library
WEB DEVELOPMENT
Basic
HTML - Hyper Text Markup Language
CSS - Cascading Style Sheets
JavaScript
An Introduction to Javascript!
How to Run JavaScript Code
Variables in Javascript
Numbers in JavaScript
JavaScript Operators
Data Types in JavaScript
Conditional Statements
Switch Statements
Loops in Javascript
Arrays in JavaScript
Strings in JavaScript
Objects in JavaScript
Object Methods in JavaScript
Functions in JavaScript
Object Referencing and Copying in JavaScript
' this' keyword
Asynchronous Programming in JavaScript
Callbacks in JavaScript
Promises in JavaScript
Constructor Functions in JavaScript
Async and Await in JavaScript
Type Conversion in Javascript
DOM
Currying in JavaScript
Network Request
Frontend
Backend
Interview Questions
FAANG QUESTIONS

