100 JavaScript Questions
100 JavaScript Questions
What is JavaScript?
ANSWERS
The "this" keyword refers to the current object context and allows
methods to access and manipulate properties and methods of the
object that called them.
Both methods allow you to call a function with a given this value
and arguments, but while call() accepts an argument list, apply()
accepts an array of arguments.
The this keyword refers to the current object in which the code is
being executed. The value of this depends on how the function is
called.
What is JavaScript?
Variables declared with var are hoisted to the top of their scope,
meaning that they can be accessed before they are declared.
Variables declared with let are not hoisted, and attempting to
access them before they are declared will result in a reference
error.
call and apply are methods that allow you to call a function with a
specific context (i.e., the value of the this keyword) and
arguments. The difference between them is how you pass the
// forEach loop
myArray.forEach(function(item) {
console.log(item);
});
You can use a for...in loop to iterate over the keys of an object.
For example:
const obj = { a: 1, b: 2, c: 3 };
for (const key in obj) {
console.log(key, obj[key]);
}
You can use methods like map(), filter(), and reduce() to create a
new array from an existing array. For example:
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
Answer: this refers to the object that the function is a method of.
It is determined at runtime and can change depending on how
the function is called.
line of code cannot begin until the previous line has finished.
Asynchronous code does not execute in a sequential order,
meaning that multiple lines of code can be executed at the same
time. Asynchronous code is often used for tasks that may take a
long time to complete, such as retrieving data from a server.
The double equals (==) and triple equals (===) operators are
used to compare values in JavaScript. The key difference between
them is that == performs type coercion, while === does not.
When using ==, JavaScript will try to convert the operands to the
same type before performing the comparison. For example, 1 ==
'1' would return true, because JavaScript converts the string '1' to
a number before comparing.
null and undefined are both values that represent the absence of
a value in JavaScript, but they have different meanings:
undefined is a value that is assigned to a variable that has not
been assigned a value or has been assigned the value undefined
explicitly. It is also returned by functions that do not have a
return statement.
Answer: The main difference between let and var is their scope.
Variables declared with var are function-scoped, while variables
declared with let are block-scoped.
Answer: Both call and apply are used to call a function with a
given this value, but call takes individual arguments, while apply
takes an array of arguments.
2. String
3. Boolean
4. Null
5. Undefined
6. Object
7. Symbol
The main difference between var, let, and const is how they
handle scope and reassignment:
● var: Variables declared with var have function scope or
global scope, and can be reassigned.
● let: Variables declared with let have block scope and can be
reassigned.
● const: Variables declared with const have block scope and
cannot be reassigned.
Undefined is a variable that has been declared but has not been
assigned a value. Null is a value that represents no value or an
empty value.
The "==" operator checks for equality of values, but it does not
check for the data type. For example, 5 == "5" will return true.
On the other hand, the "===" operator checks for both equality
of values and data type. For example, 5 === "5" will return false.
The main difference between let and var is that let has block
scope, while var has function scope. This means that variables
declared with let are only accessible within the block in which
they are defined, while variables declared with var are accessible
throughout the entire function in which they are defined.
Undefined means a variable has been declared but has not yet
been assigned a value. Null is an assignment value that
represents no value or no object.
Number("123"); // 123
parseInt("123"); // 123
Both let and var are used to declare variables in JavaScript, but
let is block-scoped while var is function-scoped. This means that
variables declared with var are accessible throughout the
function, while variables declared with let are only accessible
within the block where they are defined.
Example:
(function() {
// Code to be executed
})();
Example:
function outerFunction() {
var outerVar = 'Hello, ';
function innerFunction(name) {
console.log(outerVar + name);
}
return innerFunction;
}
var innerFunc = outerFunction();
innerFunc('John'); // Output: Hello, John
Both let and const are used to declare variables in JavaScript, but
while let can be reassigned, const cannot. This means that once a
variable is declared with const, its value cannot be changed.
Undefined means a variable has been declared but has not yet
been assigned a value, while null is an assignment value
representing no value or no object. For example:
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
function getData(callback) {
setTimeout(function() {
let data = "Some data";
callback(data);
}, 1000);
}
getData(function(data) {
console.log(data);
});
let b = 3;
b = 4;
console.log(b); // 4
The map() method creates a new array with the results of calling
a function on each element in the original array, while the
forEach() method executes a function on each element in the
original array. The map() method returns a new array, while the
forEach() method does not. For example:
let arr = [1, 2, 3];
let mappedArr = arr.map(function(elem) {
return elem * 2;
});
console.log(mappedArr); // [2, 4, 6]
arr.forEach(function(elem) {
console.log(elem * 2);
});
The this keyword refers to the object that the current function or
method is a property of. It is determined dynamically at runtime
based on how the function is called.