JavaScript Cheat Sheet
JavaScript Cheat Sheet
In the fast-paced world of tech, interviews often focus on both theoretical knowledge and
practical skills. Here's a cheat sheet that covers essential JavaScript concepts and coding
challenges, providing both code snippets and explanations.
This guide is designed to help you swiftly review and understand common topics that you're
likely to encounter during technical interviews for a developer position. It spans a range of
subjects from basic programming constructs and DOM manipulation to advanced topics like
asynchronous programming and error handling, equipping you with the insights to tackle front-
end development questions with confidence.
1. Basic Understanding:
var x = 10;
let y = 20;
const z = 30;
Answer: Variables can be defined using the var, let, or const keywords. var is
function-scoped, let and const are block-scoped. const is used for variables whose
values should not be reassigned after their initial assignment.
Can you explain the difference between let, const, and var?
Answer: var is function-scoped and can be reassigned. let and const are block-scoped.
let can be reassigned while const cannot be reassigned after the initial assignment.
function myFunction(parameters) {
// function body
}
What is the difference between function expressions and function declarations?
o Answer: Function declarations are hoisted to the top of their scope with the
function body, allowing them to be used before they're defined. Function
expressions are not hoisted, and can only be used after they're defined. The
variable holding the function expression is hoisted but assigned undefined
initially.
// Function Expression:
const myFunction = function(parameters) {
// function body
};
// Function Declaration:
function myFunction(parameters) {
// function body
}
function myFunction() {
let y = 20; // inner scope
console.log(x); // Accessible as it's in the outer scope
console.log(y); // Accessible as it's in the current scope
}
myFunction();
console.log(x); // Accessible as it's in the current scope
console.log(y); // Error as y is not accessible in the outer scope
4. DOM Manipulation:
element.addEventListener('click', function() {
// Handle click
});
5. Asynchronous JavaScript:
Can you explain what promises are and how they are used?
o Answer: Promises are objects representing the eventual completion or failure of
an asynchronous operation. They have then and catch methods for handling
success and failure, respectively.
myPromise.then((message) => {
console.log(message); // Success!
}).catch((message) => {
console.error(message); // Failure!
});
How does async/await work and when would you use it?
o Answer: async/await is syntactic sugar for working with Promises, making
asynchronous code look and behave a bit more like synchronous code. It's used
for better readability and simpler error handling.
6. Error Handling:
try {
// Some code that may throw an error
} catch (error) {
console.error(error);
}
What is the difference between a try/catch block and throwing an error?
o Answer: try/catch blocks are used to handle errors, while throw is used to
generate and throw errors.
try {
throw new Error('This is an error');
} catch (error) {
console.error(error); // Error: This is an error
}
Can you explain what arrow functions are and how they differ from regular
functions?
o Answer: Arrow functions provide a shorter syntax for writing function
expressions. They are anonymous and change the way this behaves.
What is destructuring and how can it be used to make code more readable?
o Answer: Destructuring allows unpacking values from arrays, or properties from
objects, into distinct variables, which can make code more readable and less
verbose.
const myObject = { a: 1, b: 2 };
const { a, b } = myObject;
Have you worked with any JavaScript frameworks or libraries like React, Angular,
or Vue.js?
o Answer: This would be subjective based on your personal experience. I’d
personally focus on React or Vue over Angular.
Can you build a simple to-do list app using a framework of your choice?
o Answer: template included in section 11
Can you solve the following coding challenge? Given an array of integers, find the
highest product you can get from three of the integers.
function maxProduct(arr) {
arr.sort((a, b) => b - a);
return arr[0] * arr[1] * arr[2];
}
Answer: The function maxProduct provided in the code snippet calculates the highest
product of three integers in an array by sorting the array in descending order and then
multiplying the first three elements. The assumption here is that the array contains at least
three integers.
o However, the function might not always give the correct answer because if there
are negative numbers in the array, the product of two negative numbers (which
becomes positive) and the largest positive number might be greater than the
product of the three largest positive numbers.
A more accurate solution would consider both these possibilities:
The product of the three largest numbers.
The product of the two smallest (which would be the largest
negatives) and the largest number.
The code would look something like this:
function maxProduct(arr) {
arr.sort((a, b) => a - b);
let n = arr.length;
return Math.max(arr[0] * arr[1] * arr[n - 1], arr[n - 3] * arr[n - 2] *
arr[n - 1]);
}
This modified function checks both conditions and returns the maximum of the two potential
highest products.
1. React.js:
import React, { useState } from 'react';
function TodoList() {
const [todos, setTodos] = useState([]);
const [task, setTask] = useState('');
return (
<div>
<input type="text" value={task} onChange={(e) =>
setTask(e.target.value)} />
<button onClick={addTodo}>Add</button>
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li>
))}
</ul>
</div>
);
}
2. Vanilla JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List</title>
</head>
<body>
<script>
function addTodo() {
var task = document.getElementById('task').value;
var li = document.createElement('li');
li.textContent = task;
document.getElementById('todoList').appendChild(li);
document.getElementById('task').value = '';
}
</script>
</body>
</html>
3. Svelte.js:
<script>
let todos = [];
let task = '';
function addTodo() {
todos = [...todos, task];
task = '';
}
</script>