Knowledge about Variables in JavaScript
Published: 2022-11-07 20:10:26

## JavaScript Variables
Variables serve as data storage devices.

Here are two illustrations:
1) An online store - the data may include information about products being sold and a shopping cart.
2) A chat program; data could include users, messages, and much more.

Use the let keyword in JavaScript to define a variable. We can also substitute the term "var" for the word "let" in older scripts. The word "var" is nearly identical to the word "let." A variable is also declared, albeit in a somewhat different, "classic" manner.
Let and var have a few minor differences, but we'll mostly use var.

## Code explanation of Var

**_var name = 'Argishti';_**

Here, the information "Argishti" is contained in the variable name.

**_console.log('Argishti');_**

You can refer to such a variable in your code once you have one in JavaScript. The following is an example of how you can print something with JavaScript.

**_var name = 'Argishti';

console.log(name);
_**

This will give the output of Argishti in console.

Instead of using the information explicitly, you can put the information into a variable, and use this variable instead the implicit way.

## What is Scope in Java

Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global: Global variables are those declared outside of a block. Local variables are those declared inside of a block.

## Global Scope

The global scope is the outermost scope in JavaScript. Any variable declared in global scope can be accessed anywhere.

\\ script.js

let message = "hello"

A variabe declared in local scope can only be accessed inside that local scope. That includes any other local scopes defined inside it.

Scopes are categorized into 3 types:

1.Block scope

2.Function scope

3.Module scope

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rxansvd1bshh2qzrdt1.png)

## **_Block scope_**

In JavaScript, a code block is defined using curly braces {}. Any variable declared inside is in block scope. This applies to if, for, and while statements.

## **_Function scope_**
Any variable declared inside a function is in function scope. It works for var, let, and const.
 
**_

## Module scope
_**
With the introduction of ES6, modules are a reality. Any variable defined inside a module is within the module scope and cannot be used elsewhere.

## Deep in Variables

Now that we know what scope is and also covered var, It is a god time to cover other type ov variables as well.

**LET**

In JavaScript, let is a keyword that is used to declare a block scoped variable. Usually, the var keyword is used to declare a variable in JavaScript which is treated as a normal variable, but the variables declared using the let keyword are block scoped.

**Constant**

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment. However, if a constant is an object or array its properties or items can be updated or removed.

 

## Functions in JavaScript

One of the core components of JavaScript are functions. In JavaScript, a function is comparable to a procedure—a collection of statements that carry out an action or compute a value. However, in order for a process to be considered a function, it must accept an input and produce an output with an evident connection between the input and the result.
In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want.
A function can be reused. — it can be executed as many times as you desire in a program which removes the need for code repetition.

## Types of functions in Javascript

**How to use function**

At first we need to know how to use function, and then dig deep in functions.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}

**_Function Expression_**

JavaScript allows us to assign a function to a variable and then use that variable as a function. It is called function expression.

**_Anonymous Function_**

JavaScript allows us to define a function without any name. This unnamed function is called anonymous function. Anonymous function must be assigned to a variable.

**_Nested Functions_**

In JavaScript, a function can have one or more inner functions. These nested functions are in the scope of outer function. Inner function can access variables and parameters of outer function. However, outer function cannot access variables defined inside inner functions.


**_Function Scope_**

JavaScript has function scope: Each function creates a new scope.


Variables defined inside a function are not accessible (visible) from outside the function.
Variables declared with var, let and const are quite similar when declared inside a function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3tc3auydtoc5qbd568e.png)

## How they are checking for variables

In JavaScript, the typeof operator is used to find the type of a variable, object, or method. The typeof operator returns one of the following types: number, boolean, object, string, undefined, function. To find the type of any variable, all you need to do is, write typeof > a space “ ” > and specify the variable or object whose type you want to check.