Test Answer
Test Answer
ans: undefined
10
2)
ans: 10
Since the x variable is declared without the var, let, or const keyword inside the
function foo, it becomes a reference to the outer variable x
3)
a)ans: 5,5,5,5,5
b)ans: 0,1,2,3,4
.....................................................
4)
ans: Name: Alice
Age: 30
......................................................
5)
ans:3
..............................................................
6)
ans:Hello, Anonymous!
...................................................................................
.......
7)
JavaScript has several data types, which can be categorized into two main
categories: primitive data types and reference data types.
"==" (Double Equals): Performs loose equality comparison with type coercion,
converting operands to a common type before comparison.
"===" (Triple Equals): Performs strict equality comparison without type coercion,
requiring both value and type to be the same for the comparison to be true.
var: Variables declared with var have function-level scope. This means that they
are function-scoped and are accessible throughout the whole function in which they
are declared. If a variable is declared with var outside any function, it becomes a
global variable and is accessible throughout the entire script.
let: Variables declared with let have block-level scope. This means that they are
block-scoped and are only accessible within the block in which they are declared. A
block is any code enclosed within curly braces {} like loops, conditionals, and
functions. Variables declared with let do not create properties on the global
object, making them more confined to the scope in which they are declared.
Hoisting:
var: Variables declared with var are hoisted to the top of their scope during the
creation phase of the execution context. This means that the variable declaration
is moved to the top of the function or global scope before the code execution.
However, the assignment (initialization) of the variable remains in place.
let: Variables declared with let are also hoisted, but unlike var, they are not
initialized during hoisting. Instead, they are in a "temporal dead zone" until the
line of code where they are declared is reached during the execution phase. Trying
to access a let variable before its declaration will result in a ReferenceError.
............................................................................
10)
These are self-invoking functions that are executed immediately after they are
defined.
function displayResult(result) {
console.log(`The result is: ${result}`);
}
calculate(5, 3, displayResult);
........................................................
12)
DOM stands for Document Object Model. It is a programming interface for web
documents and represents the structure of an HTML or XML document as a tree-like
data structure. The DOM allows programs and scripts to access and manipulate the
content and structure of a web page dynamically.
............................................................
13)
Arrow functions, introduced in ECMAScript 6 (ES6), are a concise and more
expressive way of writing functions in JavaScript. They provide a shorter syntax
for creating function expressions
console.log(addNumbersArrow(5, 10));
.............................................................
14)
The rest parameter and spread operator are two features introduced in ECMAScript 6
(ES6) to enhance the capabilities of handling function parameters and
arrays/objects in JavaScript.
Rest Parameter:
The rest parameter is denoted by using three dots (...) before the last parameter
of a function. It allows a function to accept an indefinite number of arguments as
an array. With the rest parameter, you can pass multiple arguments to a function
without having to explicitly define each one as a separate parameter. Inside the
function, the rest parameter becomes an array containing all the passed arguments.
function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3, 4, 5));
Spread Operator:
The spread operator is also denoted by three dots (...). It is used in different
contexts, but one of its primary uses is to spread elements from an array or object
into another array or object. The spread operator allows you to expand an array
into individual elements or combine multiple arrays/objects into one.
console.log(combinedArray);
.......................................................................
15)
Promises in JavaScript are used to handle asynchronous operations in a more
organized and efficient way. They provide a cleaner and more readable syntax for
handling the results of asynchronous tasks such as fetching data from an API,
reading files, making network requests, and more. Promises are designed to avoid
"callback hell" and allow developers to write asynchronous code that is easier to
reason about.
promis.then(
()=>{
console.log("Passed");
},
()=>{
console.log("Not Passed No Bikes");
}
)
.......................................................................
16)
a1)
let secondPersonName = people[1].name;
let secondPersonAge = people[1].age;
console.log("Name:", secondPersonName);
console.log("Age:", secondPersonAge);
..............................................
a2)
let lastPersonOccupation = people[people.length - 1].occupation;
..............................................
a3)
for (let i = 0; i < people.length; i++) {
let personName = people[i].name;
console.log("Name:", personName);
}
................................................
b1)
for (let i = 0; i < people.length; i++) {
if (people[i].name === "John") {
people[i].age = 32;
break; // Once John is found and updated, exit the loop
}
}
console.log(people);
................................................
b2)
// Find Eve's index in the array
let eveIndex = people.findIndex(person => person.name === "Eve");
................................................
b3)
// Adding a new person to the array
let newPerson = {
name: "Michael",
age: 40,
occupation: "Lawyer"
};
people.push(newPerson);
................................................
c1)
// Filtering people younger than 30
let peopleYoungerThan30 = people.filter(person => person.age < 30);
................................................
c2)
// Filtering people with occupation "Engineer"
let engineerPeople = people.filter(person => person.occupation === "Engineer");
................................................
c3)
// Filtering people older than 30 and extracting their names
let namesOlderThan30 = people
.filter(person => person.age > 30)
.map(person => person.name);