JavaScript Variables can be declared in 4 ways -
Automatically, Using var, Using let, Using constThe var keyword was used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. The var keyword should only be used in code written for older browsers. Variables are containers for storing values.
All JavaScript variables must be identified with unique names.
These unique names are called identifiers. JavaScript identifiers are case-sensitive.The "equal to" operator is written like == in JavaScript. Creating a variable in JavaScript is called "declaring" a variable. After the declaration, the variable has no value (technically it is undefined).
You can declare many variables in one statement. Start the statement with let and separate the variables by comma:
let person = "John Doe", age = 25, color = "Gray";JavaScript scoping determines the visibility and lifetime of variables within a program. Understanding scoping is essential for writing efficient and bug-free code. Hereβs an overview of the different types of scoping in JavaScript -
Global Scope -
Variables declared outside any function or block are in the global scope and are accessible from anywhere in the code. In a browser, global variables become properties of the window object.
Function Scope -
Variables declared inside a function are in the function scope and are not accessible outside the function. JavaScript functions create their own scope.
Block Scope -
Introduced in ES6, let and const allow block-level scoping. This means variables declared within a block (e.g., inside an if statement or a loop) are only accessible within that block.
Lexical Scope -
JavaScript uses lexical scoping (or static scoping), meaning the scope of a variable is determined by its position within the source code. Nested functions have access to variables declared in their outer scopes.
The this Keyword -
The value of this depends on the context in which a function is called. In the global scope, this refers to the global object (window in browsers). Inside a function, its value depends on how the function is called.
Global Scope: Variables accessible from anywhere.
Function Scope: Variables accessible only within the function.
Block Scope: Variables accessible only within the block (using let and const).
Lexical Scope: Inner functions have access to variables of their outer functions.
this Keyword: Depends on the context of the function call.Variables declared with let have Block Scope. Variables declared with let must be Declared before use. Variables declared with let cannot be Redeclared in the same scope.
let and const have block scope. let and const can not be redeclared. let and const must be declared before use. let and const does not bind to this.
Variables defined with const cannot be Redeclared, cannot be Reassigned and have Block Scope.
Variables declared with let and const are hoisted to the top of their block scope (not function or global scope), but they are not initialized. Using let variable before it is declared will result in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until it is declared. The 'var' is hoisted. The Temporal Dead Zone (TDZ) in JavaScript refers to the period of time during which a variable is declared using let or const but has not yet been initialized.
Use const when you declare - A new Array, Object, Function, RegExp.
Assignment Operator =
Addition Operator +
Multiplication Operator *
Comparison Operator >There are different types of JavaScript operators -
Arithmetic Operators
Assignment Operators
Comparison Operators
String Operators
Logical Operators
Bitwise Operators
Ternary Operators
Type OperatorsAddition +
Subtraction -
Multiplication *
Exponentiation **
Division /
Modulus(Division Remainder) %
Increment ++
Decrement --
Addition Assignment Operator +=Comparison Operators -
equal to ==
equal value and equal type ===
not equal !=
not equal value or not equal type !==
greater than >
less than <
greater than or equal to >=
less than or equal to <=
ternary operator ?JavaScript Type Operators -
typeof - Returns the type of a variable.
instanceof - Returns true if an object is an instance of an object type.Arithmetic Operators -
Addition +
Subtraction -
Multiplication *
Exponentiation (ES2016) **
Division /
Modulus (Remainder) %
Increment ++
Decrement --Assignment Operators -
A) =
B) +=
C) -=
D) =
E) /=
F) %=
G) *=JavaScript has 8 Datatypes -
String, Number, Bigint, Boolean, Undefined, Null, Symbol, ObjectJavaScript has dynamic types. This means that the same variable can be used to hold different data types. All JavaScript numbers are stored in a 64-bit floating-point format. You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression.
In JavaScript, there are 6 falsy values -
false, 0, "", null, undefined, NaN
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)