KandZ – Tuts

We like to help…!

JavaScript 4 🧬 Variables

a - JavaScript variable declaration 1 of 2
variables JavaScript are used to store and manage data
Those data can be manipulated or accessed through the code execution
They act as containers for values
They allow you to refer back to these values by their assigned name
You can declare and initialize their value at the same time
JavaScript has 3 kinds of declaration
var → The oldest way to declare variable. Variables declared with var have function scope or global scope.
They are also hoisted, which means that they are moved to the top of the containing scope during execution

b - JavaScript variable declaration 2 of 2
let → allows block-scoped variables. Variables accessible only within the block, statement or expression where they are defined. Introduced in ES6
const → is used to declare constants which are variables that cannot be reassigned after their initial assignment. Introduced in ES6
constants must be initialized at the time of declaration
const also declares block-scoped local variables
Variables can only contain letter (both uppercase and lowercase), digits, underscores (`_`), and dollar signs (`$`)
Variables cannot start with a digit
JavaScript is case-sensitive, so `myVar`, `MyVar`, and `MYVAR` are considered different variables.

c - JavaScript types of Data
variables in JavaScript can hold various types of data, primitive and reference type
Primitive Types: Boolean → true or false
String → 'hello world' Number → 1978
undefined → variable's value is not defined * null → it donates a null value
BigInt → 124312435423543645n Symbol → its instances are unique and immutable
Reference Types: Object → Collections of key-value pairs
Array → lists of values used for storing multiple items under a single variable name
Function → Reusable blocks of code that perform specific tasks, accepting inputs and optionally returning outputs.

d - JavaScript Variable Scope
JavaScript variables can belong to 1 of the 3 following scopes:
Global Scope → Variables declared outside any function or block are global
They are accessible throughout the entire script.
Function Scope → Variables declared inside a function
They are only accessible within that function.
Block Scope → Variables declared inside a block `{` `}`
They are only accessible within that block.
Such blocks are switch statement, try...catch statement, for statement, function body and static initiliaztion block

Leave a Reply