What are the key features of JavaScript?
Dynamic and weak typing
Closures
allow inner functions to access variables from their outer function scopes.
First-class functions
Functions can be:
How do you include JavaScript code in an HTML file?
inline: You can write JavaScript directly within the HTML file using the script tag and placing the code between the opening and closing tags.
External file: You can also link an external JavaScript file using the src attribute of the script tag.
What are the primitive data types in JavaScript?
Explain the concept of hoisting in JavaScript.
What are the different ways to define variables in JavaScript?
How do you check the data type of a variable in JavaScript
typeof
For example:
let num = 10; console.log(typeof num); // Outputs: "number"
.
message = "Hello"; console.log(typeof message); // Outputs: "string"
.
let isValid = true; console.log(typeof isValid); // Outputs: "boolean"
undefined
null
What are the arithmetic operators in JavaScript?
What is the purpose of the “this” keyword in JavaScript?
For example:
const person = {
name: "John",
sayHello: function() {
console.log("Hello, " + this.name);
}
};
person.sayHello(); // Outputs: "Hello, John"In the above code, this.name accesses the name property of the person object using the this keyword.
Explain the concept of closures in JavaScript.
An inner function always has access to the variables defined in the outer functions even if the outer function has finished executing.
For Example:
function outer() {
let outerVariable = "I'm from the outer function";
function inner() {
console.log(outerVariable);
}
return inner;
}
const closureFunction = outer();
closureFunction(); // Outputs: "I'm from the outer function"In the above code, the inner function forms a closure with the outer function’s scope. Even after the outer function has returned, the closureFunction still has access to the outerVariable variable.
What is event delegation in JavaScript?
How does prototypal inheritance work in JavaScript?
For example:
// Prototype object
const animal = {
sound: "Sound not defined",
makeSound: function() {
console.log(this.sound);
}
};
// Object inheriting from the prototype
const dog = Object.create(animal);
dog.sound = "Woof";
dog.makeSound(); // Outputs: "Woof"
// Object inheriting from the same prototype
const cat = Object.create(animal);
cat.sound = "Meow";
cat.makeSound(); // Outputs: "Meow"In the above code, both the dog and cat objects inherit the sound property and makeSound method from the animal prototype object using Object.create()
What is the purpose of the “use strict” directive in JavaScript?
How do you create an object in JavaScript?
What are the different ways to loop through an array in JavaScript?
”==”
It attempts to convert the operands to a common type before making the comparison.
”===”
It returns true only if the values are equal and the types match.
NOTE: It does not perform type coercion.
What is the purpose of the “setTimeout” function in JavaScript?
for example:
function sayHello() {
console.log("Hello!");
}
setTimeout(sayHello, 2000); // Executes sayHello after a 2-second delayHow do you handle errors in JavaScript?
For example:
try {
// Code that may throw an error
const result = 10 / 0;
console.log(result);
} catch (error) {
// Code to handle the error
console.log("An error occurred: " + error.message);
}Explain the concept of asynchronous programming in JavaScript.