Javascript quick revision

Point-1: var vs let vs const – What's the Difference?
In JavaScript, var, let, and const are used to declare variables.
There are 3 factors to analyze the differences between these keywords
Scope of variables:
Redeclaration and reassignment
Hoisting
| Feature | var | let | const |
| Scope | Function scope (accessible throughout the function they are declared in) | Block scope (accessible within the block they are declared in) | Block scope (accessible within the block they are declared in) |
| Redeclaration | Allowed (can be redeclared in the same scope) | Not allowed (results in SyntaxError) | Not allowed (results in SyntaxError) |
| Reassignment | Allowed (can be reassigned) | Allowed (can be reassigned) | Not allowed (cannot be reassigned) |
| Hoisting | Hoisted and initialized with undefined | Hoisted but not initialized | Hoisted but not initialized |
Point-2: Datatypes in javascript
In JavaScript, data types specify the type of data that can be stored and manipulated within the language. JavaScript is a loosely typed or a dynamic language, meaning variables do not need to be declared with a data type explicitly, and their data types can change during the execution of the program. JavaScript supports two categories of data types:
Primitive Data Types: These are the basic data types that hold a single value.
String: Represents textual data, e.g.,
"Hello, World!".Number: Represents both integer and floating-point numbers, e.g.,
42,3.14.BigInt: Represents integers with arbitrary precision, e.g.,
9007199254740991n.Boolean: Represents a logical entity with two values:
trueandfalse.Undefined: Represents a variable that has not been assigned a value.
Null: Represents the intentional absence of any object value.
Symbol: Represents a unique, immutable identifier, often used as the key of an Object property.
Reference Data Types (Objects): These data types are used to store collections of data or more complex entities.
Object: Represents instances of named values, e.g.,
{ name: "Alice", age: 30 }.Array: A type of object used for storing sequences of values, e.g.,
[1, 2, 3].Function: Represents code that can be called by other code or by itself, e.g.,
function hello() { console.log("Hello, World!"); }.
JavaScript dynamically determines the data type based on the value assigned to the variable, allowing for a flexible approach to programming but also requiring careful management to avoid unexpected behavior or bugs.
Point-3: undefined vs null
In JavaScript, undefined and null both represent absence of value, but in different ways:
Keypoint to remember: undefined represents the system-level, absent value, null is used by programmers to indicate the intentional absence of any object value.
Here's a table view summarizing the differences between undefined and null in JavaScript:
| Aspect | undefined | null |
| Description | Indicates that a variable has not been assigned a value. | Explicitly represents the absence of any object value. |
| Type | "undefined" | "object" (considered a legacy bug) |
| Equality | undefined == null is true (non-strict equality) | undefined == null is true (non-strict equality) |
undefined === null is false (strict equality) | undefined === null is false (strict equality) | |
| Default Value | Default value for uninitialized variables. | Can be assigned to a variable as a representation of no value. |
| Function Return | Functions return undefined if no value is specified. | Can be returned by a function to explicitly indicate no meaningful value. |
| Usage | Used by the JavaScript engine to denote absence of value. | Used by programmers to denote absence of value intentionally. |