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

Test Answer

s1

Uploaded by

Shreyas Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Test Answer

s1

Uploaded by

Shreyas Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1)

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.

Primitive Data Types:

Number: Represents numeric values, including integers and floating-point numbers.


String: Represents textual data, enclosed in single ('') or double ("") quotes.
Boolean: Represents a logical value, either true or false.
Undefined: Represents a variable that has been declared but not assigned a value.
Null: Represents the intentional absence of any object value.
Symbol (ES6): Represents a unique and immutable value, often used as object
property keys.

Reference Data Types:

Object: Represents a collection of key-value pairs or properties. Objects can be


created using object literals {}, constructors, or classes.
Array: Represents an ordered collection of elements. Arrays are a special type of
object with numeric indices.
Function: Represents a reusable block of code that can be called with specified
arguments.
Date: Represents a specific moment in time and provides methods to work with dates
and times.
RegExp: Represents a regular expression, used for pattern matching within strings.

// Primitive data types


let numberVar = 42; // Number
let stringVar = "Hello, World!"; // String
let boolVar = true; // Boolean
let undefinedVar; // Undefined
let nullVar = null; // Null
let symbolVar = Symbol("mySymbol"); // Symbol (ES6)

// Reference data types


let objectVar = { key: "value" }; // Object
let arrayVar = [1, 2, 3]; // Array
let functionVar = function() {}; // Function
let dateVar = new Date(); // Date
let regexVar = /[A-Za-z]+/; // RegExp
..............................................................................
8)

"==" (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.

console.log(5 == "5"); // true, number 5 is converted to string "5" for


comparison
console.log(true == 1); // true, boolean true is converted to number 1 for
comparison
console.log(null == undefined); // true, both null and undefined are loosely equal
.............................................................................
9)
Scoping:

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.

const result = (function(a, b) {


return a + b;
})(5, 3);
console.log(result);
..............................................................................
11)
Callback are functions passed as arguments to other functions and are invoked at a
later time or when a specific event occurs.
function calculate(num1, num2, callback) {
const result = num1 + num2;
callback(result);
}

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

const addNumbersArrow = (a, b) => a + b;

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.

let array1 = [1, 2, 3];


let array2 = [4, 5, 6];
let combinedArray = [...array1, ...array2];

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.

const promis=new Promise((resolve,reject)=>{


console.log("Async Execution");
if(false){
resolve();
}
else{
reject();
}
});

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;

console.log("Occupation of the last person:", lastPersonOccupation);

..............................................
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");

// Update Eve's occupation


if (eveIndex !== -1) {
people[eveIndex].occupation = "Graphic Designer";
}

// Printing the updated array


console.log(people);

................................................
b3)
// Adding a new person to the array
let newPerson = {
name: "Michael",
age: 40,
occupation: "Lawyer"
};

people.push(newPerson);

// Printing the updated array


console.log(people);

................................................
c1)
// Filtering people younger than 30
let peopleYoungerThan30 = people.filter(person => person.age < 30);

// Printing the filtered array


console.log(peopleYoungerThan30);

................................................
c2)
// Filtering people with occupation "Engineer"
let engineerPeople = people.filter(person => person.occupation === "Engineer");

// Printing the filtered array


console.log(engineerPeople);

................................................
c3)
// Filtering people older than 30 and extracting their names
let namesOlderThan30 = people
.filter(person => person.age > 30)
.map(person => person.name);

// Printing the array of names


console.log(namesOlderThan30);

You might also like