Skip to main content

Command Palette

Search for a command to run...

Javascript quick revision

Updated
3 min read
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

  1. Scope of variables:

  2. Redeclaration and reassignment

  3. Hoisting

Featurevarletconst
ScopeFunction 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)
RedeclarationAllowed (can be redeclared in the same scope)Not allowed (results in SyntaxError)Not allowed (results in SyntaxError)
ReassignmentAllowed (can be reassigned)Allowed (can be reassigned)Not allowed (cannot be reassigned)
HoistingHoisted and initialized with undefinedHoisted but not initializedHoisted 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:

  1. 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: true and false.

    • 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.

  2. 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:

Aspectundefinednull
DescriptionIndicates that a variable has not been assigned a value.Explicitly represents the absence of any object value.
Type"undefined""object" (considered a legacy bug)
Equalityundefined == 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 ValueDefault value for uninitialized variables.Can be assigned to a variable as a representation of no value.
Function ReturnFunctions return undefined if no value is specified.Can be returned by a function to explicitly indicate no meaningful value.
UsageUsed by the JavaScript engine to denote absence of value.Used by programmers to denote absence of value intentionally.