Basic is the foundation of all programming language ๐
Questions ๐ค
let
vsconst
vsvar
- What is
Temporal Dead Zone
orTDZ
? How that Occur? - How
let
work in for a loop when let doesn't allow reassignment? - is
var
Dead? What should I use? - How
let
andconst
is scoped in Js? - What is the purpose of
use strict
? - What does the double negation operator
!!
do? ==
vs===
- What are the falsy values in js
- What is
Coercion
with an example :::
var
, let
and const
A Story of var: An old school technique to declare a variable
let: An alternative to var and its a modern way to declare any variable
const: Const values never gonna be replaced once it's declared
The Story of Var ๐
A variable can be declared as var
or let
. But even if we declare the variable with let, var is beast in it own way
๐ธ var
has no block scope meaning it can be accessed globally (its subjective to strict mode)
If a code block is inside a function then the scope will remain to the function level
If var
declared outside any function or block that instance will be assigned to the global object. which means a window
object inside the browser and global
object in the node
๐ธ var
do tolerate re-declaration
๐ธ var
variable(s) can be declared below their use
This behavior is called Hoisting
(Raising) where all var
is hoisted at the global context
- the Only declaration can be Hoist, not an assignment
- During the execution, all the variable declaration will go at the top of the execution context :::
let
it go
๐ธ let has a sensible scoping. Once the scope is declared the scope will remain in its block
๐ธ let don't allow re-declaration
๐ธ If we declare variable with let it will not be attached to the global scope hence it will not be reachable outside the file
๐ธ let in the loop can re-bind in each iteration making sure to reassign the value from the end of the previous iteration
What the heck ๐คฏ how is that even working when let won't allow re-assignment ๐ค
TBU Add behind the scene of for loop with let :::
const
How about the ๐ธ const variables are immutable, once declare you cant change
๐ธ Scope of the const variable will remain inside the block
๐ธ const variables are not hoisted
๐ธ we can't reach to the const by this
keyword
๐ธ If you declare const
to an object you will not be able to delete that object rather you will be able to declare, reassign or even delete value of the constant object
Hey I'm very strict ๐ฎ๐ปโโ๏ธ
In the ES6 module system, strict mode is turned on by default. use strict
is a literal expression to enable strict mode in coding. This strict context prevents certain actions from being taken and throws more exceptions.
๐ธIn early javascript, it was allowed to use write code however we want
but when you use strict mode at the top of the strict it's not possible to write code however we want
๐ธStrict mode will help to prevent the unexpected errors, exception which can arise during runtime of the application
๐ธuse strict is very helpful to avoid a conflict between variables. If we missed declaring it will notify a developer
Some consideration of strict mode
๐ธ Variables canโt be left undeclared
๐ธ Function parameters must have unique names (or are considered syntax errors)
๐ธ with
keyword is forbidden
๐ธ Errors are thrown on assignment to read-only
properties
๐ธ Octal numbers like 00840
are syntax errors
๐ธ Attempts to delete
undeletable properties throw an error
๐ธ delete prop is a syntax error, instead of assuming delete global[prop]
๐ธ eval
doesnโt introduce new variables into its surrounding scope
๐ธ eval
and arguments
canโt be bound or assigned to
๐ธ arguments
doesnโt magically track changes to method parameters
๐ธ arguments.callee
throws a TypeError, no longer supported
๐ธ arguments.caller
throws a TypeError, no longer supported
๐ธ Context passed as this in method invocations is not boxed
(forced) into becoming an Object
๐ธ No longer able to use fn.caller
and fn.arguments
to access the JavaScript stack
๐ธ Reserved words (e.g protected, static, interface, etc) cannot be bound
Remember ๐ง
use strict
should always declare at the top of a script to enable throughout the script or declare inside a block to enable only for a block. It won't work if we declare somewhere else- There is no way to cancel a strict mode
- Strict mode can be eliminated from a modern scripting language, In fact, modern language and framework internally does it for you :::
Untold story of an operator ๐ง๐ปโ๐
Both == and === operator serve the same purpose which is to check the object equality
==
Operator
๐ธ== is operator is very loose. It only care about a content even if type is mismatch it wont boughter
Behind the scene, javascript convert the string into a number and the equate them this is called COERCION
===
Operator
๐ธ === is super strict. It want both content and type should equal
Same goes for !=
and !==
which perform same things but negate
!!
Double negation Operator
๐ธ Suppose you have an expression which gives result & if you want that result to be boolean (true/false) then !!
is the solution
๐ธ If you want to get True
or False
from Not a string, 0, empty string, undefined, NAN or something else
๐ธ In reality, there is no !!
operator in javascript. Its just one negates after another negate. It just negates the result then negate it again.
List of falsy values in Javascript
Falsey Value | Description |
---|---|
false | The keyword false |
0 | The number zero |
-0 | The number negative zero |
0n | BigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy. |
"" | Empty string value without space |
null | Absence of any value |
undefined | The primitive value where the value is not defined |
NaN | Not a Number |