വീണ്ടുമുണരട്ടെ
undefined and not defined in JS
undefined
undefined is a property of the global object. That is, it is a variable in global scope. The initial value of undefined is the primitive value undefined.
A variable is ‘declared’, it has its own placeholder (memory is allcoated) but not having the value of itself ‘defined’ hence ‘undefined’. Until the variable has assigned a value, the ‘undefined’ fills that particular placeholder and ‘undefined’ is itself a datatype.
Not Defined
This case comes in error where js engine neither find that particular variable nor its placeholder and cannot find the variable in 1st phase of context (Memory allocation context)
JS is loosely typed language, this means that JavaScript will figure out what type of data you have and make the necessary adjustments so that you don’t have to redefine your different types of data.
References
Intersection Observer API
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document’s viewport.
Syntax to create Intersection Observer API
const observer = new IntersectionObserver(entries => {
console.log(entries)
})
References
Pure functions
A pure function is a function which:
Given the same input, always returns the same output and the Produces have no side effects.
Preoperties
- No random values
- No current date/time
- No global state
- No mutation of parameters
Benefits
- Self-documenting
- Easily testable
ഓർമ്മകളിലേക്ക്
Primitive Data types in JS
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types:
- string
- number
- bigint
- boolean
- undefined
- symbol
- null
All primitives are immutable; that is, they cannot be altered. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned to a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.
References
Shortest Program in JS
- Shortest Program in JS: Empty file. Still, browsers make global Execution context and global space along with Window object.
- Global scope: Anything that is not in a function, is in the global space.
- Variables present in a global space can be accessed by a “window” object. (like window.a)
- In global scope, (this === window) object. For example refer the below,
var a = 10;
Console.log(windows.a) // 10
Console.log(a) // 10
Console.log(this.a) // 10
Class and Function Hoisting in JS
An important difference between function declarations and class declarations is that while functions can be called in code that appears before they are defined, classes must be defined before they can be constructed.
class hoisting
Classes defined using a class declaration are hoisted, which means that JavaScript has a reference to the class. However the class is not initialized by default, so any code that uses it before the line in which it is initialized is executed will throw a ReferenceError.
const p = new Rectangle(); // ReferenceError
class Rectangle {}
This occurs because while the class is hoisted its values are not initialized.
Function and class expression hoisting
Function expressions and class expressions are not hoisted.