0% found this document useful (0 votes)
5K views

Javascript?books

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

Javascript?books

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

@KhushiChoudhary

@KhushiChoudhary
// Output: "number"
// Output: "string"
// Output: "boolean"
// Output: "object"
// Output: "function"

@KhushiChoudhary
// Output: true

// Output: false

@KhushiChoudhary
@KhushiChoudhary
@KhushiChoudhary
return a + b;
};

return a + b;
};

@KhushiChoudhary
value: 42,
valueOfThis: function() {
return this.value; // 'this' refers to the object
calling the function (obj1)
}
};

// Call the method


console.log(obj1.valueOfThis()); // Output: 42

@KhushiChoudhary
value: 84,
valueOfThis: () => {
return this.value; // 'this' does not refer to
obj2; it inherits from the parent scope (window in
this case)
}
};

// Call the method


console.log(obj2.valueOfThis()); // Output: undefined
or an error (depending on the environment)

@KhushiChoudhary
// Outputs "undefined", not

// Outputs "Hello, world!"


function sayHello() {
console.log("Hello, world!");
}

@KhushiChoudhary
Even though sayHello is defined after its call, JavaScript acts as if it were
declared at the beginning of the scope, enabling its execution.

result = 100; // Hoisted within the function


console.log(result); // Outputs 100
var result;
}

performTask();

@KhushiChoudhary
EASY

'use strict';
x = 'Test message';
console.log(x);
}
strict_function(); // ReferenceError: x is not defined

@KhushiChoudhary
@KhushiChoudhary
@KhushiChoudhary
@KhushiChoudhary
return number => number * factor;
}

@KhushiChoudhary
@KhushiChoudhary
@KhushiChoudhary
@KhushiChoudhary
When a function is a method of an object, this refers to that object.

@KhushiChoudhary
myMethod: function() {
console.log(this); // refers to myObject
}
};

myObject.myMethod();

this.property = 'some value';


}

const myInstance = new MyClass();


console.log(myInstance.property); // 'some value'

@KhushiChoudhary
console.log(globalVar); // Accessible inside the
function
}
exampleFunction();
console.log(globalVar); // Accessible outside the
function

@KhushiChoudhary
var localVar = "I am local";
console.log(localVar); // Accessible inside the
function
}

exampleFunction();
// console.log(localVar); // This would result in an error because localVar
is not accessible outside the function

@KhushiChoudhary
var localVar1 = 777;

var innerFunction1 = function(){


console.log(localVar1); // Accesses localVar1 inside
innerFunction1, outputs 777
}

var innerFunction2 = function(){


console.log(globalVar); // Accesses globalVar inside
innerFunction2, outputs 42
}

innerFunction1();
innerFunction2();
}

mainFunction();

@KhushiChoudhary
@KhushiChoudhary
// Outer function scope
let outerVariable = 10;

function innerFunction() {
// Inner function scope
let innerVariable = 5;
// Accessing both inner and outer variables
console.log("Inner Variable:", innerVariable);
console.log("Outer Variable:", outerVariable);
}

// Returning the inner function, creating a closure

return innerFunction;
}
// Calling outerFunction returns innerFunction,
which is now a closure
let closureFunction = outerFunction();

// Executing the closure function


closureFunction();

@KhushiChoudhary
@KhushiChoudhary
console.log(greeting + ' ' + this.name);
}

const person = { name: 'John' };

sayHello.call(person, 'Hello'); // Outputs: Hello


John

@KhushiChoudhary
console.log(greeting + ' ' + this.name);
}

const person = { name: 'John' };

sayHello.apply(person, ['Hello']); // Outputs: Hello


John

@KhushiChoudhary
console.log(greeting + ' ' + this.name);
}

const person = { name: 'John' };

const sayHelloToJohn = sayHello.bind(person);

sayHelloToJohn('Hello'); // Outputs: Hello John

@KhushiChoudhary
console.log(greeting + ' ' + this.name);

@KhushiChoudhary
total += value;
}

// Example usage of the impure function


addToTotal(5);
console.log(total); // Output: 5

@KhushiChoudhary
this.name = name;
}

// Add a method to the prototype, shared by all


Person objects:
Person.prototype.greet = function() {

console.log("Hello, my name is " + this.name);


};

@KhushiChoudhary
// Both objects can access the greet method from the
prototype:
person1.greet(); // Output: "Hello, my name is
Alice"
person2.greet(); // Output: "Hello, my name is Bob"

@KhushiChoudhary
console.log("Welcome, " + name + "! How can we
assist you today?");
}

function outerFunction(callback) {
let name = prompt("Please enter your name.");
callback(name);
}

outerFunction(customGreeting);

@KhushiChoudhary
getProfile(user.id, function(profile) {
getPosts(user.id, function(posts) {
displayUserProfile(user, profile, posts,
function() {
// More nested callbacks...
});
});
});
});

@KhushiChoudhary
console.log(value1); // Outputs: undefined
console.log(value2); // Throws a ReferenceError
var value1 = 1;
let value2 = 2;
}

@KhushiChoudhary
// Perform asynchronous operations

@KhushiChoudhary
return numbers.reduce((total, num) => total + num,
0);
}

console.log(sum(1, 2, 3, 4, 5)); // Output: 15

@KhushiChoudhary
// code here
}

// code here
}

@KhushiChoudhary
yield 1;
yield 2;
yield 3;
} // Creating a generator
const generator = simpleGenerator();
// Using the generator to get values
console.log(generator.next()); // { value: 1, done: false }
console.log(generator.next()); // { value: 2, done:
false }
console.log(generator.next()); // { value: 3, done:
false }

@KhushiChoudhary
console.log(generator.next()); // { value: undefined,

@KhushiChoudhary
hoists it to the top of the current scope.N

return a + b;
}

@KhushiChoudhary
return a + b;
};

@KhushiChoudhary
console.log('This will be executed after 1000
milliseconds');
}, 1000);

@KhushiChoudhary
console.log('This will be executed in the next
iteration of the event loop');
});

console.log('This will be executed in the next


event loop cycle');
});

@KhushiChoudhary
Follow For More
@KhushiChoudhary

You might also like