Posts

Showing posts from April, 2025

Call, Applay and Bind

 Call  function greet ( name , age ){     return `my name is ${ name } and age ${ age } ` } let result = greet ( "pankaj" , 100 ); console . log ( result ); Here, you're simply calling the greet() function with arguments directly. This works fine, and there's no need for call() in this case because you're just invoking the function as usual. Use Call  function greet ( name , age ){     console . log ( `my name is ${ name } and age ${ age } ` ) } let result = greet ( "pankaj" , 100 ); let result2 = greet ( "raju" , 200 ); console . log ( result ); console . log ( result2 ); function greet1 ( name , age ){     console . log ( `my name is ${ this . name } and age ${ this . age } ` )  } let person1 = { name : "Raju" , age : 11 , } let person2 = {     name : "pankaj" ,     age : 10 } console . log ( "CALL............" ) greet1 . call ( person1 ) greet1 . call ( person2 ) function greet(name, age)...

Currying

Currying means breaking a function that takes multiple arguments into a series of functions that each take one argument son take father property or grandfather property (StepByStep) function Granfather ( GFP ){     return function ( FP ){         return function son (){         return `My owner ship is ${ GFP } and ${ FP } `         }     } } let result = Granfather ( 10 )( 100 ); console . log ( result ()); Currying Function  1. Break function 2. Execute first Break, Out put of first Break as input Similar 

Clousure and Lexcical Scope

 Example  function father (){     let Property = "House"       return function son (){         let sonProperty = "Car"         return `My owner ship in ${ sonProperty } and ${ Property } `      } } const result = father ();   // returns the 'son' function console . log ( result ()); Closure  A closure is created when an inner function ( son ) retains access to variables from its outer function ( father ) even after the outer function has finished executing. In this case, son still has access to Property even though father() has already returned. That's a closure . Lexcical  Lexical Scope means a function's scope is determined by its position in the code (its location in the source file), and it has access to variables declared in its outer scopes . Important  Closure + Loop ( Classic Interview Example ) for ( var i = 0 ; i < 3 ; i ++ ) { ...

Shallow Copy and Deep Copy in JavaScript

Image
Shallow Copy in JavaScript When we use the spread operator ( ... ) or methods like Object.assign() or slice() to copy an object or array, it creates a shallow copy . This means only the top-level properties are copied, while nested objects or arrays are still referenced , not cloned. 🧪 Example with Array let a = [ 10 , 20 , [ 30 , 40 ]]; let b = [ ... a ]; // Shallow copy b [ 0 ] = 100 ; console . log ( a ); // [10, 20, [30, 40]] console . log ( b ); // [100, 20, [30, 40]] Here , b is a shallow copy of a . When we change b [ 0 ], it doesn ’ t affect the original . So far , so good . But now , let ’ s try changing the nested array b [ 2 ][ 0 ] = 300 ; console . log ( a ); // [10, 20, [300, 40]] console . log ( b ); // [100, 20, [300, 40]] Now both a and b are affected! That’s the problem with shallow copies — the nested values are shared by reference . Deep copy solves the problem of modifying nested arrays without affecting the original array. For ...

Spread and Rest

Image
 Spread and Rest Spread and Rest boths are donated by (...)  three doted USES:  Both are use in arrya and object  SPREAD Spread  is use to expand the array and object. 1. Spread behave with array  As mentioned above, the ... syntax is used to expand values. In this case, arr1 is being expanded inside arr2 using the spread operator. 2. Spread behave with object Obj2 is expand so obj behave like as a spread operator  Rest  Rest Operator with array    1. The rest operator is used to combine multiple values into a single array. It’s especially useful when we don’t know how many arguments a function will receive. function sum(...numbers) {   return numbers.reduce((a, b) => a + b, 0); } sum(1, 2, 3);  Rest with Object const user = {   name: 'Sameer',   age: 25,   location: 'India',   profession: 'Developer' }; const { name, ...rest } = user;