What is JavaScript?
- JavaScript is a high-level, interpreted programming language that is primarily used for adding interactivity and dynamic behavior to web pages.
What are the key features of JavaScript?
- JavaScript is lightweight and interpreted.
- It supports both procedural and object-oriented programming paradigms.
- It is designed for embedding within HTML pages.
- It supports dynamic typing and automatic memory management.
What are the data types in JavaScript?
- Primitive data types: String, Number, Boolean, Undefined, Null, Symbol (added in ECMAScript 6).
- Object data type: Object.
What is the difference between
null
andundefined
?null
represents the intentional absence of any object value.undefined
represents the absence of a defined value.
What is the use of the
typeof
operator in JavaScript?- The
typeof
operator is used to determine the data type of a variable or expression.
- The
Explain the difference between
==
and===
operators.- The
==
operator checks for equality after type coercion. - The
===
operator checks for equality without type coercion (strict equality).
- The
What are truthy and falsy values in JavaScript?
- Truthy values are values that evaluate to true in a Boolean context.
- Falsy values are values that evaluate to false in a Boolean context.
What is the difference between
let
,var
, andconst
for declaring variables?var
is function-scoped,let
andconst
are block-scoped.- Variables declared with
var
can be re-declared and updated, while variables declared withlet
can be updated but not re-declared. - Variables declared with
const
cannot be updated or re-declared.
Explain hoisting in JavaScript.
- Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during compilation.
What are closures in JavaScript?
- Closures are functions that have access to variables from their outer scope even after the outer function has finished executing.
What are higher-order functions?
- Higher-order functions are functions that can take other functions as arguments or return functions as results.
Explain the concept of callback functions.
- Callback functions are functions passed as arguments to other functions, which are then invoked or executed inside the outer function.
What are promises in JavaScript?
- Promises are objects representing the eventual completion or failure of an asynchronous operation. They allow asynchronous code to be written in a more synchronous fashion.
What is async/await in JavaScript?
- Async/await is a modern syntax for working with asynchronous code. It allows asynchronous functions to be written in a more synchronous style, making asynchronous code easier to read and maintain.
What is the Event Loop in JavaScript?
- The Event Loop is a mechanism in JavaScript that allows asynchronous code to be executed in a non-blocking manner. It continuously checks the call stack and the task queue, moving tasks from the queue to the stack when the stack is empty.
Explain prototypal inheritance in JavaScript.
- Prototypal inheritance is a way of creating objects where one object inherits properties and methods from another object, known as its prototype.
What is the
this
keyword in JavaScript?- The
this
keyword refers to the context within which a function is called. Its value is determined by how a function is invoked.
- The
What are arrow functions in JavaScript?
- Arrow functions are a concise way of writing function expressions in JavaScript. They have a shorter syntax compared to traditional function expressions and lexically bind
this
.
- Arrow functions are a concise way of writing function expressions in JavaScript. They have a shorter syntax compared to traditional function expressions and lexically bind
Explain the difference between
forEach
,map
,filter
, andreduce
methods.forEach
: Executes a provided function once for each array element.map
: Creates a new array by applying a function to each element in the original array.filter
: Creates a new array with elements that pass a test specified by a function.reduce
: Applies a function to each element of the array, resulting in a single output value.
What are modules in JavaScript?
- Modules are reusable pieces of code that encapsulate related functionality and can be exported from one script and imported into another.
What is the DOM (Document Object Model)?
- The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of an HTML document as a tree of nodes, allowing developers to interact with and manipulate the document dynamically.
How do you select elements in the DOM?
- Elements in the DOM can be selected using methods like
getElementById
,getElementsByClassName
,getElementsByTagName
,querySelector
, andquerySelectorAll
.
- Elements in the DOM can be selected using methods like
How do you create and append elements to the DOM dynamically?
- Elements can be created dynamically using methods like
createElement
,createTextNode
, andappendChild
, and then appended to the DOM using methods likeappendChild
andinsertBefore
.
- Elements can be created dynamically using methods like
What are event listeners in JavaScript?
- Event listeners are functions that are executed in response to a specific event occurring on an HTML element, such as a click, hover, or keypress.
How do you add event listeners to DOM elements?
- Event listeners can be added to DOM elements using methods like
addEventListener
or by assigning event handler properties likeonclick
,onmouseover
, etc.
- Event listeners can be added to DOM elements using methods like
What is event propagation in JavaScript?
- Event propagation refers to the process by which an event is passed through the DOM hierarchy, from the target element to its ancestors (capturing phase) and then back down to the target element (bubbling phase).
What is event delegation in JavaScript?
- Event delegation is a technique where a single event listener is attached to a parent element to handle events for all its descendants. It leverages event propagation to efficiently handle events for dynamically created elements.
What are the different phases of event propagation?
- There are three phases of event propagation: capturing phase, target phase, and bubbling phase.
What is the difference between
event.target
andevent.currentTarget
?event.target
refers to the element that triggered the event.event.currentTarget
refers to the element to which the event listener is attached.
What are the most commonly used events in JavaScript?
- Some commonly used events in JavaScript include
click
,mouseover
,keydown
,submit
,load
,DOMContentLoaded
, etc.
- Some commonly used events in JavaScript include
What is asynchronous programming in JavaScript?
- Asynchronous programming is a programming pattern that allows multiple tasks to be performed concurrently, without blocking the execution of other code.
What are callbacks in JavaScript?
- Callbacks are functions passed as arguments to other functions and are
What are the different data types in JavaScript?
- JavaScript has several data types, including:
- Primitive types: string, number, boolean, null, undefined, symbol (added in ECMAScript 6).
- Reference types: object, array, function.
- JavaScript has several data types, including:
What is the difference between
==
and===
in JavaScript?- The
==
operator checks for equality after converting both operands to a common type, whereas the===
operator checks for equality without type conversion (strict equality).
- The
What is a closure in JavaScript?
- A closure is a function that has access to its own scope, as well as the scope of its parent function, even after the parent function has closed. Closures are created whenever a function is defined within another function.
What is the difference between
let
,var
, andconst
?var
is function-scoped and can be redeclared and reassigned.let
is block-scoped and can be reassigned but not redeclared.const
is block-scoped and cannot be reassigned or redeclared.
What is hoisting in JavaScript?
- Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, regardless of where they are declared in the code.
What are the different ways to create objects in JavaScript?
- Objects in JavaScript can be created using object literals, constructor functions, the
Object.create()
method, and ES6 classes.
- Objects in JavaScript can be created using object literals, constructor functions, the
What is the difference between
null
andundefined
in JavaScript?null
represents the intentional absence of any value and is explicitly assigned.undefined
represents an uninitialized or non-existent value and is automatically assigned to variables that have not been assigned a value or to properties that do not exist in an object.
What is the difference between
==
and===
operators?- The
==
operator checks for equality after performing type coercion, while the===
operator checks for strict equality without type coercion. In other words,===
requires both the value and the type to be the same for equality.
- The
What is an IIFE (Immediately Invoked Function Expression)?
- An IIFE is a JavaScript function that is executed immediately after it is defined. It is typically used to create a separate scope for variables and avoid polluting the global namespace.
Explain event bubbling and event capturing.
- Event bubbling is the process where an event triggered on a child element propagates up through its ancestors in the DOM hierarchy. Event capturing is the opposite, where the event is captured by the parent elements first before reaching the target element.
What is a callback function?
- A callback function is a function passed as an argument to another function, which is then invoked inside the outer function to complete some kind of action or operation.
What is the difference between
var
,let
, andconst
?var
is function-scoped and can be redeclared and reassigned.let
andconst
are block-scoped,let
can be reassigned but not redeclared, whileconst
cannot be reassigned or redeclared.
What are arrow functions?
- Arrow functions are a shorter syntax for writing function expressions in JavaScript. They have a more concise syntax and automatically bind the
this
value based on the surrounding lexical context.
- Arrow functions are a shorter syntax for writing function expressions in JavaScript. They have a more concise syntax and automatically bind the
What is destructuring in JavaScript?
- Destructuring is a feature in JavaScript that allows you to extract values from arrays or objects and assign them to variables in a concise and efficient way using syntax like
const { prop1, prop2 } = object
.
- Destructuring is a feature in JavaScript that allows you to extract values from arrays or objects and assign them to variables in a concise and efficient way using syntax like
What is the difference between
let
,const
, andvar
?- Answer:
let
andconst
are block-scoped declarations introduced in ES6, whilevar
is function-scoped. Variables declared withlet
can be reassigned, but their scope is limited to the block in which they are defined. Variables declared withconst
cannot be reassigned and have block scope.var
declarations can be reassigned and have function scope.
- Answer:
What are the different ways to define a function in JavaScript?
- Answer: Functions in JavaScript can be defined using function declarations, function expressions, arrow functions, and methods (functions that are properties of objects).
What is the purpose of
this
keyword in JavaScript?- Answer: The
this
keyword refers to the object to which a function belongs or is bound at the time of its invocation. Its value is determined by how a function is called, and it allows functions to access and manipulate the properties of the object they are associated with.
- Answer: The
Explain the difference between
==
and===
operators in JavaScript.- Answer: The
==
operator compares two values for equality after performing type conversion, while the===
operator compares both value and type without type conversion. For example,5 == '5'
would be true, but5 === '5'
would be false.
- Answer: The
How does prototypal inheritance work in JavaScript?
- Answer: JavaScript objects have a prototype property, which points to another object. When you try to access a property on an object and it’s not found, JavaScript looks up the prototype chain until it finds the property or reaches the end of the chain.
Explain the event delegation pattern in JavaScript.
- Answer: Event delegation is a technique where you attach an event handler to a parent element rather than to individual child elements. Events that occur on the child elements are handled by the parent element. This is useful for dynamically created elements or when you have many similar elements with the same event.
How do you handle asynchronous operations in JavaScript?
- Answer: Asynchronous operations in JavaScript can be handled using callbacks, promises, or async/await
What is the difference between
let
,const
, andvar
in JavaScript?- Answer:
let
andconst
are block-scoped, whilevar
is function-scoped.const
is used for variables that should not be re-assigned, whilelet
allows re-assignment.
- Answer:
What are the different ways to define a function in JavaScript? Explain each.
- Answer: Functions in JavaScript can be defined using function declarations, function expressions, arrow functions, and ES6 class methods.
What is event bubbling and event capturing in JavaScript?
- Answer: Event bubbling is when an event is first captured by the innermost element and then propagated to outer elements. Event capturing is the opposite: the event is captured by the outermost element first and then propagated to the inner element
What is JavaScript, and what are its key features?
- JavaScript is a high-level, interpreted programming language primarily used for adding interactivity to web pages. Its key features include lightweight nature, dynamic typing, and support for both object-oriented and functional programming paradig
Explain the difference between
undefined
andnull
in JavaScript.undefined
is a primitive value automatically assigned to variables that have not been assigned a value, whilenull
is an intentional absence of any value and must be assigned explicitly.
Explain the concept of scope in JavaScript.
- Scope refers to the accessibility of variables and functions in different parts of your code during runtime. JavaScript has global scope, function scope, and block scope.
What are the differences between
let
,const
, andvar
?let
andconst
have block scope, whilevar
has function scope or global scope.- Variables declared with
var
can be re-declared and updated,let
allows re-assignment but not re-declaration, andconst
does not allow re-assignment or re-declaration after initialization.
What is JavaScript and what are its key characteristics?
- JavaScript is a high-level, interpreted programming language primarily used for adding interactivity to web pages. Its key characteristics include being lightweight, dynamically typed, and supporting both object-oriented and functional programming paradigms.
Explain the concept of variable hoisting in JavaScript.
- Variable hoisting is a JavaScript behavior where variable declarations are moved to the top of their containing scope during the compilation phase. However, only the declaration is hoisted, not the initialization.
Explain the event loop in JavaScript and how it handles asynchronous operations.
- The event loop is a mechanism that allows JavaScript to perform non-blocking I/O operations by offloading operations to the browser’s APIs while continuing to execute the code synchronously.
What are the differences between
==
and===
operators in JavaScript?==
is the equality operator that performs type coercion before comparing two values, whereas===
is the strict equality operator that checks both value and type without coercion.
What is the purpose of the
use strict
directive in JavaScript?- The
'use strict';
directive enables strict mode in JavaScript, which helps catch common coding errors and prevents certain unsafe actions.
- The
How do you declare and initialize an array in JavaScript?
- Arrays can be declared and initialized using square brackets (
[]
). Example:let myArray = [1, 2, 3];
- Arrays can be declared and initialized using square brackets (
What is a promise in JavaScript? How does it differ from callbacks?
- A promise is an object representing the eventual completion or failure of an asynchronous operation. Promises provide a cleaner alternative to callback-based asynchronous code, offering better error handling and chaining capabilities.
Explain the concept of prototypal inheritance in JavaScript.
- Prototypal inheritance is a mechanism in JavaScript where objects inherit properties and methods from other objects through prototypes. Each object has an internal link to another object called its prototype, forming a prototype chain.
How do you create an object in JavaScript?
- Objects in JavaScript can be created using object literals (
{}
), constructor functions, or theObject.create()
method.
- Objects in JavaScript can be created using object literals (
Explain the concept of event bubbling and event capturing in JavaScript.
- Event bubbling is the propagation of an event from the target element up through its ancestors to the document root, while event capturing is the opposite—the event is captured by the outermost ancestor first.
How do you handle errors in JavaScript?
- Errors in JavaScript can be handled using
try-catch
blocks, where code that may throw an error is placed inside thetry
block and any potential errors are caught and handled in thecatch
block.
- Errors in JavaScript can be handled using
What are arrow functions in JavaScript? Provide an example.
- Arrow functions are a concise way to write function expressions in JavaScript, introduced in ES6
What are template literals in JavaScript? Provide an example.
- Template literals are string literals allowing embedded expressions. They are enclosed by backticks (`).
What are the different ways to loop over arrays in JavaScript?
- Arrays in JavaScript can be looped over using
for
loops,forEach()
method,for...of
loop, or array methods likemap()
,filter()
, etc.
- Arrays in JavaScript can be looped over using
How do you check the type of a variable in JavaScript?
- The
typeof
operator is used to determine the type of a variable. Example:typeof 42; // "number"
- The
What are the primitive data types in JavaScript?
- Primitive data types in JavaScript include string, number, boolean, null, undefined, bigint, and symbol
Explain the concept of hoisting in JavaScript.
- Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compilation phase.
What is the difference between
undefined
andnull
?undefined
means a variable has been declared but not yet assigned a value, whereasnull
is an assignment value representing the absence of any object value.
How do you declare a variable in JavaScript?
- Variables in JavaScript can be declared using the
var
,let
, orconst
keywords.
- Variables in JavaScript can be declared using the
What are the different ways to create an array in JavaScript?
- Arrays in JavaScript can be created using array literals
[]
, theArray()
constructor, or by converting other data types usingArray.from()
.
- Arrays in JavaScript can be created using array literals
What is the purpose of the
this
keyword in JavaScript?- The
this
keyword refers to the object on which a method is being invoked or the context in which a function is executed. Its value is determined by how a function is called.
- The
How do you iterate over elements of an array in JavaScript?
- You can iterate over elements of an array using loops like
for
,for...of
, or array methods likeforEach()
,map()
,filter()
, etc.
- You can iterate over elements of an array using loops like
Explain the difference between
let
,const
, andvar
.let
andconst
were introduced in ES6 and have block scope, whilevar
has function scope.let
allows re-assignment but not re-declaration,const
does not allow re-assignment or re-declaration after initialization, andvar
allows both.
What is the difference between an arrow function and a regular function in JavaScript?
- Arrow functions are anonymous functions with a more concise syntax and lexical
this
. They do not have their ownthis
context and cannot be used as constructors.
- Arrow functions are anonymous functions with a more concise syntax and lexical
How do you handle form validation in JavaScript?
- Form validation in JavaScript can be handled by accessing form elements, listening for form submission events, and validating input fields using conditions or regular expressions.
What is JSON and how do you parse JSON in JavaScript?
- JSON (JavaScript Object Notation) is a lightweight data interchange format. JSON can be parsed into JavaScript objects using the
JSON.parse()
method.
- JSON (JavaScript Object Notation) is a lightweight data interchange format. JSON can be parsed into JavaScript objects using the
Explain event bubbling and event capturing in JavaScript.
- Event bubbling is the propagation of an event from the innermost target element up to the root of the DOM, while event capturing is the opposite, starting from the root down to the target element.
How do you create a class in JavaScript?
- Classes in JavaScript can be created using the
class
keyword, and class methods can be defined using the class method syntax.
- Classes in JavaScript can be created using the
What is the difference between
document.getElementById()
anddocument.querySelector()
?document.getElementById()
returns the element with the specified ID attribute, whiledocument.querySelector()
returns the first element that matches a specified CSS selector.
What are the different types of errors in JavaScript?
- JavaScript errors can be categorized into syntax errors, runtime errors, and logical errors.
How do you clone an object in JavaScript?
- Objects can be shallow cloned using methods like
Object.assign()
orspread
syntax (...
). For deep cloning, libraries like Lodash or utilities likeJSON.parse(JSON.stringify(obj))
can be used.
- Objects can be shallow cloned using methods like
What is event delegation and why is it useful?
- Event delegation is a technique where a single event listener is attached to a parent element to handle events that occur on its children. It’s useful for improving performance and handling dynamically created elements.
What is the difference between
push()
andconcat()
methods in JavaScript arrays?- The
push()
method adds one or more elements to the end of an array and returns the new length of the array, while theconcat()
method is used to merge two or more arrays and returns a new array.
- The
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.
- You can use the
What is a higher-order function in JavaScript?
- A higher-order function is a function that takes another function as an argument or returns a function as a result.
Explain the concept of currying in JavaScript.
- Currying is the process of transforming a function that takes multiple arguments into a sequence of functions that each take a single argument. It allows partial application of a function.
What is the
call()
method in JavaScript?- The
call()
method is used to invoke a function with a specifiedthis
value and arguments provided individually.
- The
What is the purpose of the
apply()
method in JavaScript?- The
apply()
method is similar to thecall()
method, but it accepts
- The
Explain the event loop in JavaScript.
- Answer: The event loop is a mechanism in JavaScript that handles asynchronous operations. It continuously checks the call stack and the callback queue. When the call stack is empty, it takes the first function from the queue and pushes it onto the stack.
What are closures and how are they used?
- Answer: Closures are functions that retain access to variables from their lexical scope even after the scope has closed. They are commonly used to create private variables and encapsulate functionality.