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

Javascripts

The document provides a comprehensive overview of JavaScript, covering fundamental concepts such as variables, data types, functions, and error handling. It explains key features like hoisting, closures, asynchronous operations, and the use of promises and async/await. Additionally, it discusses advanced topics like prototypal inheritance, classes, and the differences between null and undefined.

Uploaded by

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

Javascripts

The document provides a comprehensive overview of JavaScript, covering fundamental concepts such as variables, data types, functions, and error handling. It explains key features like hoisting, closures, asynchronous operations, and the use of promises and async/await. Additionally, it discusses advanced topics like prototypal inheritance, classes, and the differences between null and undefined.

Uploaded by

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

Javascripts questions

1. What is JavaScript?
JavaScript is a high-level, interpreted programming language used for adding
interactivity to web pages.

2. What are the differences between JavaScript and Java?


JavaScript is a scripting language primarily used for web development, whereas
Java is a general-purpose programming language.

3. How do you declare variables in JavaScript?


Variables in JavaScript can be declared using the var , let , or const keyword.
Example:

javascriptCopy code
var age = 25;
let name = "John";
const PI = 3.14;

4. Explain the different data types in JavaScript.


JavaScript has six primitive data types: number , string , boolean , null , undefined ,
and symbol , and one non-primitive data type: object .
Example:

javascriptCopy code
let num = 10;
let name = "Alice";
let isActive = true;
let person = { name: "John", age: 30 };
let emptyValue = null;
let notDefined = undefined;
let id = Symbol("id");

5. What are the scope and lifetime of a variable in JavaScript?


The scope of a variable determines where it can be accessed in code, and the
lifetime refers to the duration it exists in memory.

Javascripts questions 1
Example:

javascriptCopy code
function printAge() {
var age = 25; // Function scope variable
console.log(age);
}

printAge();
console.log(age); // Error: 'age' is not defined (outside the function)

6. What is hoisting in JavaScript?


Hoisting is a behavior where variable and function declarations are moved to the top
of their respective scope during the compilation phase.
Example:

javascriptCopy code
console.log(x); // Output: undefined
var x = 5;

7. How do you create a function in JavaScript?


Functions in JavaScript can be created using function declarations or function
expressions.

Example:

javascriptCopy code
// Function Declaration
function greet() {
console.log("Hello!");
}

// Function Expression
const sayHi = function() {
console.log("Hi!");
};

Javascripts questions 2
8. Explain the difference between function declarations and function
expressions.
Function declarations are hoisted and can be called before their declaration,
whereas function expressions cannot be hoisted.

Example:

javascriptCopy code
// Function Declaration
greet(); // Output: "Hello!"
function greet() {
console.log("Hello!");
}

// Function Expression
sayHi(); // Error: 'sayHi' is not a function
const sayHi = function() {
console.log("Hi!");
};

9. What are arrow functions in JavaScript, and how do they differ from regular
functions?
Arrow functions are a shorthand syntax for defining functions and do not have their
own 'this' binding.

Example:

javascriptCopy code
// Regular Function
function add(a, b) {
return a + b;
}

// Arrow Function
const multiply = (a, b) => a * b;

10. How do you handle errors in JavaScript?


Errors in JavaScript can be handled using try-catch blocks.

Example:

Javascripts questions 3
javascriptCopy code
try {
// Code that may throw an error
throw new Error("This is an example error");
} catch (error) {
console.log(error.message); // Output: "This is an example error"
}

11. What is the use of the "strict mode" in JavaScript?


Strict mode is a mode that enforces stricter rules and checks in JavaScript, helping
to write cleaner and safer code.
Example:

javascriptCopy code
"use strict";
// Strict mode code here

12. How do you work with arrays in JavaScript?


Arrays in JavaScript can be created and manipulated using various methods.

Example:

javascriptCopy code
let numbers = [1, 2, 3, 4, 5];
console.log(numbers[0]); // Output: 1

numbers.push(6); // Add element to the end


numbers.pop(); // Remove element from the end
numbers.splice(1, 2); // Remove elements at index 1 and 2

13. Explain the different methods to add elements to an array in JavaScript.


Elements can be added to an array using push() , unshift() , and splice() methods.

Example:

javascriptCopy code
let fruits = ["apple", "orange"];

Javascripts questions 4
fruits.push("banana"); // ["apple", "orange", "banana"]
fruits.unshift("grape"); // ["grape", "apple", "orange", "banana"]
fruits.splice(1, 0, "pear"); // ["grape", "pear", "apple", "orange", "banana"]

14. How do you work with objects in JavaScript?


Objects in JavaScript consist of key-value pairs, and you can access or modify the
values using the key.

Example:

javascriptCopy code
let person = {
name: "John",
age: 30,
isEmployed: true
};

console.log(person.name); // Output: "John"

person.age = 35;
console.log(person.age); // Output: 35

15. What is prototypal inheritance in JavaScript?


Prototypal inheritance is a way of creating objects based on other objects, where
one object acts as the prototype for another.

Example:

javascriptCopy code
// Base Object (Prototype)
let animal = {
sound: "Animal sound",
makeSound: function() {
console.log(this.sound);
}
};

// Derived Object (Child)


let dog = Object.create(animal);
dog.sound = "Bark";
dog.makeSound(); // Output: "Bark"

Javascripts questions 5
16. How do you create and use classes in JavaScript?
Classes in JavaScript can be created using the class keyword and used to
instantiate objects.

Example:

javascriptCopy code
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

greet() {
console.log(`Hello, my name is ${this.name}, and I am ${this.age} years old.`);
}
}

let john = new Person("John", 30);


john.greet(); // Output: "Hello, my name is John, and I am 30 years old."

17. Explain the "this" keyword in JavaScript.


The "this" keyword refers to the current object or context in which the code is
executed.

Example:

javascriptCopy code
const person = {
name: "John",
sayName: function() {
console.log(`My name is ${this.name}.`);
}
};

person.sayName(); // Output: "My name is John."

18. What are closures in JavaScript, and how are they used?
Closures are functions that have access to variables from their outer (enclosing)
function even after the outer function has finished executing.

Example:

Javascripts questions 6
javascriptCopy code
function outerFunction() {
let count = 0;
return function() {
count++;
console.log(count);
};
}

let counter = outerFunction();


counter(); // Output: 1
counter(); // Output: 2

19. How do you handle asynchronous operations in JavaScript?


Asynchronous operations in JavaScript can be handled using callbacks, promises,
async/await, and event listeners.
Example (using callback):

javascriptCopy code
function fetchData(callback) {
setTimeout(() => {
const data = "Data fetched successfully";
callback(data);
}, 2000);
}

fetchData((result) => {
console.log(result); // Output: "Data fetched successfully"
});

20. What is the event loop in JavaScript?


The event loop is a mechanism in JavaScript that handles asynchronous tasks and
callbacks.

Example:

javascriptCopy code
console.log("Start");

setTimeout(() => {
console.log("Callback executed");
}, 1000);

Javascripts questions 7
console.log("End");

// Output: "Start", "End", "Callback executed" (after 1 second)

21. Explain callback functions in JavaScript.


A callback function is a function passed as an argument to another function and
executed after the completion of the operation.

Example:

javascriptCopy code
function calculate(a, b, callback) {
let result = a + b;
callback(result);
}

function displayResult(result) {
console.log(`The result is: ${result}`);
}

calculate(5, 10, displayResult); // Output: "The result is: 15"

22. What are promises in JavaScript, and how do they work?


Promises are objects that represent the eventual completion or failure of an
asynchronous operation and provide a more structured way of handling async
operations.
Example:

javascriptCopy code
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = "Data fetched successfully";
resolve(data);
}, 2000);
});
}

fetchData()
.then((result) => {
console.log(result); // Output: "Data fetched successfully"
})

Javascripts questions 8
.catch((error) => {
console.error(error);
});

23. How do you use async/await in JavaScript?


The async/await feature allows you to write asynchronous code in a synchronous
style, making it easier to read and understand.
Example:

javascriptCopy code
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = "Data fetched successfully";
resolve(data);
}, 2000);
});
}

async function getData() {


try {
const result = await fetchData();
console.log(result); // Output: "Data fetched successfully"
} catch (error) {
console.error(error);
}
}

getData();

24. What are generators in JavaScript?


Generators are functions that can be paused and resumed during execution,
allowing for lazy evaluation of data.
Example:

javascriptCopy code
function* generateNumbers() {
yield 1;
yield 2;
yield 3;
}

Javascripts questions 9
const generator = generateNumbers();
console.log(generator.next().value); // Output: 1
console.log(generator.next().value); // Output: 2
console.log(generator.next().value); // Output: 3

25. How do you work with maps and sets in JavaScript?


Maps and sets are collection types introduced in ES6 for storing unique values and
key-value pairs, respectively.
Example (using Map):

javascriptCopy code
const person1 = { name: "John" };
const person2 = { name: "Alice" };

const contacts = new Map();


contacts.set(person1, "[email protected]");
contacts.set(person2, "[email protected]");

console.log(contacts.get(person1)); // Output: "[email protected]"


console.log(contacts.get(person2)); // Output: "[email protected]"

26. What are rest and spread operators in JavaScript?


The rest operator ( ... ) is used to collect multiple elements into an array, while the
spread operator is used to spread elements from an array or object.
Example:

javascriptCopy code
// Rest Operator
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}

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

// Spread Operator
const numbers = [1, 2, 3, 4, 5];
console.log(...numbers); // Output: 1 2 3 4 5

Javascripts questions 10
27. Explain the "use strict" directive in JavaScript.
"use strict" is a directive used to enforce a stricter set of rules and error handling in
JavaScript code.

Example:

javascriptCopy code
"use strict";

function displayMessage() {
message = "This is an error";
console.log(message);
}

displayMessage(); // Error: "message is not defined"

28. How do you clone an object in JavaScript?


Objects can be cloned using the Object.assign() method or the spread operator.
Example:

javascriptCopy code
const originalObject = { name: "John", age: 30 };

// Using Object.assign()
const clonedObject1 = Object.assign({}, originalObject);

// Using spread operator


const clonedObject2 = { ...originalObject };

29. What is the difference between null and undefined in JavaScript?


null is an explicit value representing the absence of any object value, while

undefined is a primitive value that represents the absence of a value.


Example:

javascriptCopy code
let x = null;
let y;
console.log(x); // Output: null
console.log(y); // Output: undefined

Javascripts questions 11
30. How do you check for the existence of a property in an object?
You can use the hasOwnProperty() method or the in operator to check if an object
has a specific property.
Example:

javascriptCopy code
const person = { name: "John", age: 30 };

console.log(person.hasOwnProperty("name")); // Output: true


console.log("name" in person); // Output: true
console.log(person.hasOwnProperty("address")); // Output: false
console.log("address" in person); // Output: false

31. What are template literals in JavaScript?


Template literals are a way to create strings with embedded expressions, making it
easier to concatenate variables into strings.

Example:

javascriptCopy code
const name = "John";
const age = 30;

const message = `My name is ${name}, and I am ${age} years old.`;


console.log(message); // Output: "My name is John, and I am 30 years old."

32. How do you handle default parameters in JavaScript functions?


Default parameters can be set using the assignment operator ( = ) in function
parameters.
Example:

javascriptCopy code
function greet(name = "Guest") {
console.log(`Hello, ${name}!`);
}

greet(); // Output: "Hello, Guest!"

Javascripts questions 12
greet("John"); // Output: "Hello, John!"

33. What are the different methods to copy an array in JavaScript?


Arrays can be copied using methods like slice() , concat() , and the spread
operator.
Example (using slice):

javascriptCopy code
const numbers = [1, 2, 3, 4, 5];
const copiedArray = numbers.slice();

34. How do you convert a string to a number in JavaScript?


Strings can be converted to numbers using parseInt() or parseFloat() functions.
Example:

javascriptCopy code
const numString = "10";
const num = parseInt(numString);
console.log(num); // Output: 10

35. How do you convert a number to a string in JavaScript?


Numbers can be converted to strings using the toString() method or the string
interpolation.
Example:

javascriptCopy code
const num = 10;
const numString = num.toString();
console.log(numString); // Output: "10"

36. What is the difference between "==" and "===" operators in JavaScript?
The "==" operator checks for equality after performing type coercion, while the
"===" operator checks for strict equality without type coercion.

Javascripts questions 13
Example:

javascriptCopy code
console.log(1 == "1"); // Output: true
console.log(1 === "1"); // Output: false

37. How do you check if a variable is an array in JavaScript?


You can use the Array.isArray() method to check if a variable is an array.
Example:

javascriptCopy code
const numbers = [1, 2, 3];
console.log(Array.isArray(numbers)); // Output: true

const name = "John";


console.log(Array.isArray(name)); // Output: false

38. What are closures in JavaScript, and how are they used?
Closures are functions that have access to variables from their outer (enclosing)
function even after the outer function has finished executing.

Example:

javascriptCopy code
function outerFunction() {
let count = 0;
return function() {
count++;
console.log(count);
};
}

let counter = outerFunction();


counter(); // Output: 1
counter(); // Output: 2

39. How do you handle asynchronous operations in JavaScript?


Asynchronous operations in JavaScript can be handled using callbacks, promises,

Javascripts questions 14
async/await, and event listeners.
Example (using callback):

javascriptCopy code
function fetchData(callback) {
setTimeout(() => {
const data = "Data fetched successfully";
callback(data);
}, 2000);
}

fetchData((result) => {
console.log(result); // Output: "Data fetched successfully"
});

40. What is the event loop in JavaScript?


The event loop is a mechanism in JavaScript that handles asynchronous tasks and
callbacks.

Example:

javascriptCopy code
console.log("Start");

setTimeout(() => {
console.log("Callback executed");
}, 1000);

console.log("End");

// Output: "Start", "End", "Callback executed" (after 1 second)

41. Explain callback functions in JavaScript.


A callback function is a function passed as an argument to another function and
executed after the completion of the operation.
Example:

javascriptCopy code
function calculate(a, b, callback) {
let result = a + b;

Javascripts questions 15
callback(result);
}

function displayResult(result) {
console.log(`The result is: ${result}`);
}

calculate(5, 10, displayResult); // Output: "The result is: 15"

42. What are promises in JavaScript, and how do they work?


Promises are objects that represent the eventual completion or failure of an
asynchronous operation and provide a more structured way of handling async
operations.
Example:

javascriptCopy code
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = "Data fetched successfully";
resolve(data);
}, 2000);
});
}

fetchData()
.then((result) => {
console.log(result); // Output: "Data fetched successfully"
})
.catch((error) => {
console.error(error);
});

43. How do you use async/await in JavaScript?


The async/await feature allows you to write asynchronous code in a synchronous
style, making it easier to read and understand.
Example:

javascriptCopy code
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {

Javascripts questions 16
const data = "Data fetched successfully";
resolve(data);
}, 2000);
});
}

async function getData() {


try {
const result = await fetchData();
console.log(result); // Output: "Data fetched successfully"
} catch (error) {
console.error(error);
}
}

getData();

44. What are generators in JavaScript?


Generators are functions that can be paused and resumed during execution,
allowing for lazy evaluation of data.

Example:

javascriptCopy code
function* generateNumbers() {
yield 1;
yield 2;
yield 3;
}

const generator = generateNumbers();


console.log(generator.next().value); // Output: 1
console.log(generator.next().value); // Output: 2
console.log(generator.next().value); // Output: 3

45. How do you work with maps and sets in JavaScript?


Maps and sets are collection types introduced in ES6 for storing unique values and
key-value pairs, respectively.
Example (using Map):

javascriptCopy code
const person1 = { name: "John" };
const person2 = { name: "Alice" };

Javascripts questions 17
const contacts = new Map();
contacts.set(person1, "[email protected]");
contacts.set(person2, "[email protected]");

console.log(contacts.get(person1)); // Output: "[email protected]"


console.log(contacts.get(person2)); // Output: "[email protected]"

46. What are rest and spread operators in JavaScript?


The rest operator ( ... ) is used to collect multiple elements into an array, while the
spread operator is used to spread elements from an array or object.
Example:

javascriptCopy code
// Rest Operator
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}

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

// Spread Operator
const numbers = [1, 2, 3, 4, 5];
console.log(...numbers); // Output: 1 2 3 4 5

47. Explain the "use strict" directive in JavaScript.


"use strict" is a directive used to enforce a stricter set of rules and error handling in
JavaScript code.

Example:

javascriptCopy code
"use strict";

function displayMessage() {
message = "This is an error";
console.log(message);
}

displayMessage(); // Error: "message is not defined"

Javascripts questions 18
48. How do you clone an object in JavaScript?
Objects can be cloned using the Object.assign() method or the spread operator.

Example:

javascriptCopy code
const originalObject = { name: "John", age: 30 };

// Using Object.assign()
const clonedObject1 = Object.assign({}, originalObject);

// Using spread operator


const clonedObject2 = { ...originalObject };

49. What is the difference between null and undefined in JavaScript?


null is an explicit value representing the absence of any object value, while

undefined is a primitive value that represents the absence of a value.

Example:

javascriptCopy code
let x = null;
let y;
console.log(x); // Output: null
console.log(y); // Output: undefined

50. How do you check for the existence of a property in an object?


You can use the hasOwnProperty() method or the in operator to check if an object
has a specific property.
Example:

javascriptCopy code
const person = { name: "John", age: 30 };

console.log(person.hasOwnProperty("name")); // Output: true


console.log("name" in person); // Output: true
console.log(person.hasOwnProperty("address")); // Output: false
console.log("address" in person); // Output: false

Javascripts questions 19

You might also like