JavaScript Flashcards
What is Javascript?
A high-level interpreted programming language that allows developers to add behavior to web pages
Let vs Const
let allows reassignment
const does not allow reassignment after declaration
var is no longer used
What is the difference between == & === ?
Quality operators.
== checks for equality regardless of type
=== checks for value and type equality
Explain prototypal inheritance
When we read a property from an object, and it’s missing, JS takes it from the hidden property - prototype
Example:
let animal = {eats: true};
let rabbit = {jumps: true};
rabbit.__proto__ = animal
alert( rabbit.eats ); //true
alert( rabbit.jumps ); //true
Explain closure
The combination of a function bundled together (enclosed) with references to its surrounding state (lexical environment)
Closure gives you access to an outer function’s scope from an inner function
Explain event delegation
A single event listener is attached to a common ancestor, and events are handled based on the target element
Explain the Event Loop
A mechanism that handles async operations by queuing them up, and executing them in the order they are queued
This keeps the main thread free to handle user interactions
Explain the bind method
A method used so that an object can borrow a method from another object
For example, if an object has a function that another object needs to use, you can bind it instead of duplicating functionality
Explain asynchronous programming
Allows the program to perform other tasks while waiting for async operations to complete.
Uses mechanisms like callbacks (older), promises, and async/await
Callback vs Promise
Callback: functions passed as args to other functions, and are executed after a task is completed.
* Can pass data from parent to child
* Runs after parent completes
Promise: objects representing the eventual completion or failure of an async operation.
* fulfilled - success
* rejected - failure
* pending - not fulfilled/rejected
* settled - fulfilled or rejected
Explain hoisting
Variable and function declarations are moved to the top of their scope during compilation
Allows them to be used before they’re declared
Explain the this keyword
Refers to the current execution context
Used to access or modify the props of the current object
Arrow vs Regular Functions
Arrow:
* simple, shorter, can skip the return keyword
* do not use ‘this’ or ‘arguments’
* cannot be used as constructors
const multiply = (num1, num2) => num1 * num2
Regular:
* have more flexibility
* can be used in more scenarios
Explain array.map
Creates a new array by applying a provided function to each element of an existing array
Explain array.filter
Creates a new array with elements that pass a specific test implemented by the provided function
Explain array.forEach
iterates over elements of an array and executes a provided function once for each element
Does not create a new array
Explain array.reduce
Reduces elements of an array to a single value.
Takes a callback function and an optional initial value, then the callback is applied to each array element, accumulating the result.
Explain debouncing
Used to optimize event handling by delaying the execution of a function for a specified period of time, or period of inactivity
Explain throttling
Used to optimize event handling by limiting how often a function can be called in a period of time
Explain error handling
Errors are handled using try/catch blocks.
The code that may throw an error is contained in the try block, and the error handling is contained in the catch block
Explain local storage
A web storage object for storing key value pairs locally on the client’s browser
It persists after the browser is closed