js_notes
js_notes
{
var a = 10;
let b = 20;
const c = 30;
console.log(a);
console.log(b);
console.log(c);
}
console.log(a);
console.log(b);
console.log(c);
---------------------------------------------------------------
SHADOWING -----
var a = 100;
{
var a = 10;
let b = 20;
const c = 30;
console.log(a);
console.log(b);
console.log(c);
}
Output -- 10,20,30
var a = 100;
{
var a = 10;
let b = 20;
const c = 30;
console.log(a);
console.log(b);
console.log(c);
}
console.log(a);
Output -- 10,20,30,10
let b = 100;
{
var a = 10;
let b = 20;
const c = 30;
console.log(a);
console.log(b);
console.log(c);
}
console.log(b);
Output -- 10,20,30,100
const c = 100;
{
var a = 10;
let b = 20;
const c = 30;
console.log(a);
console.log(b);
console.log(c);
}
console.log(c);
Output -- 10,20,30,100
const c = 100;
function x() {
const c = 30;
console.log(c);
}
console.log(c);
Output -- 100
const c = 100;
function x() {
const c = 30;
console.log(c);
}
x();
console.log(c);
Output -- 30,100
----------------------------------------------------------------------------
let a = 20;
{
var a = 20;
}
Error = a has already been declared
var a = 20;
{
let a = 20;
}
No error thrown
---------------------------------------------------------------------------------
CLOSURES ------
function x(){
var a=7;
function y(){
console.log(a);
}
y();
}
x();
Output ---- 7
function x(){
var a=7;
function y(){
console.log(a);
}
return y;
}
var z = x();
console.log(z);
//...................
z();
function x(){
var i = 1;
settimeout(function(){
console.log(i);
}, 1000); // 1000 means output shows after 1 sec
console.log("Namaste Javascript"); // It print first cannot wait for timer
}
x();
-----------------------------------------------------------------------------------
---------------
function a() {
console.log("a called");
}
function () {
}
Basicaly the values which we passed inside a function (1, 2) are arguments
and these labels or identifiers (param1, param2)
which get those values are parameters.
When they are treated as a value passed into another functions or returned
from another functions so this is known as
first class functions.
Here now it's upto x when it wants to call the y so that is like this
function y is called back sometime later in your code
so thats why it is known as callback function
setTimeout (function() {
console.log("Timer");
}, 5000 )
function x(y){
console.log("x");
y();
}
x(function y(){
console.log("y");
})
Output ---- x y
Output (After 5 sec) ---- x y Timer
-----------------------------------------------------------------------------------
----------------------