0% found this document useful (0 votes)
2 views

js_notes

The document discusses various JavaScript concepts including block scope, shadowing, illegal shadowing, closures, function declarations, expressions, anonymous functions, named function expressions, parameters vs arguments, first-class functions, callback functions, and higher-order functions. It provides examples and outputs for each concept to illustrate their behavior and differences. The document serves as a comprehensive guide to understanding these fundamental JavaScript programming concepts.

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

js_notes

The document discusses various JavaScript concepts including block scope, shadowing, illegal shadowing, closures, function declarations, expressions, anonymous functions, named function expressions, parameters vs arguments, first-class functions, callback functions, and higher-order functions. It provides examples and outputs for each concept to illustrate their behavior and differences. The document serves as a comprehensive guide to understanding these fundamental JavaScript programming concepts.

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

BLOCK SCOPE -----

{
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);

Output ---- 10,20,30

---------------------------------------------------------------

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

----------------------------------------------------------------------------

ILLEGAL SHADOWING -----

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();

Output ---- f y(){


console.log(a);
}
7

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();

output ---- Namaste Javascript


1

-----------------------------------------------------------------------------------
---------------

FUNCTION STATEMENT or Func DECLARATION ------

function a() {
console.log("a called");
}

FUNCTION EXPRESSION ----


var b = function () {
console.log("b called");
}
a();
b();

ANONYMOUS FUNCTION ------


(Anonymous Function are used in a place where function are used as a value
otherwise it will throw syntax error)

function () {
}

NAMED FUNCTION EXPRESSION ------

var b = function xyz () {


console.log(xyz); // Here It prints the function
}
a();
b();
xyz(); // Here it gives error xyz is not defined

DIFFERENCE BETWEEN PARAMETERS AND ARGUMENTS ----------

var b = function (param1, param2) { //Here param1,param2 are


parameters and cannot be accessed outside
console.log("b called");
}
a();
b(1,2); //Here 1,2 are arguments

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.

FIRST CLASS FUNCTIONS ------

The ability of functions to be used as values and can be pass this in an


argument to another functions and can be returned
from the functions is this ability is known as first class functions

When they are treated as a value passed into another functions or returned
from another functions so this is known as
first class functions.

CALLBACK FUNCTIONS --------


function x(y){
}
x(function y(){ //Here y is callback function
})

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

Callback function in asynchronous task ----

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

-----------------------------------------------------------------------------------
----------------------

HIGHER ORDER FUNCTIONS --------

A function which takes another function as an argument or return a function


from it is known as higher order functions.

function x(){ // x is callback function


console.log("Hello');
}

function y(x){ // y is higher order function


x();
}

You might also like