Skip to content

Frontend Interview Cheatsheet. Short summarized answers to the most popular Interview Questions. Daily updates in Telegram Channel: https://t.me/zenfrontend

Notifications You must be signed in to change notification settings

tmdautov/frontend-interview-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 

Repository files navigation

Frontend Interview Cheatsheet

Frontend Interview Survival Cheatsheet

JS theory

What are the data types in JavaScript?

There are 8 basic data types in JavaScript

  • Primitive (7 types)
    • String
    • Number
    • Boolean
    • BigInt
    • null
    • undefined
    • Symbol
  • Non-primitive (1 type)
    • Object: This is the base type for more complex structures like Arrays, Functions, and other objects

What is the Hoisting?

Hoisting is a Javascript default behavior of moving functions, variables or classes declarations on top of the module/function level scope before executing the code.

  • Function declarations are hoists, while function expressions are not
// This is function declaration
function foo() { ... }

// This is function expression
var bar = function () { ... }
  • Variables hoisting
    • Variables declared with var hoisted to the top of its functional or global scope, but the initialization is not. If you try to use a variable before it is declared and initialized, it will have the value undefined.
    • Variables declared with let and const hoisted, but they are not initialized with a default value. Accessing them before the actual declaration results in a ReferenceError. This is often referred to as the "temporal dead zone."
console.log(a); // undefined, due to hoisting
var a = 5;

console.log(b); // ReferenceError: b is not defined
let b = 3;

What is the Closure?

  • Closure is a function that has access to its own scope, the scope of the outer function, and the global scope.

  • Closures can be used to emulate private properties and methods.

  • Every JavaScript function creates a new lexical environment. This environment has a reference to its outer environment, forming a scope chain

  • The scope chain is a process of how Javascript looks for variables. When code needs to access a variable, JavaScript looks at the current scope. If not found, it moves up the scope chain to the outer scope and continues until it reaches the global scope. If the variable is still not found, a reference error is thrown.

Example: Counter

function getCounter() {
  let counter = 0;
  return function() {
    return counter++;
  }
}

let count = getCounter();
console.log(count()); // 0
console.log(count()); // 1
console.log(count()); // 2

What is the Event Loop?

The event loop is a single-threaded loop that monitors the call stack and the callback queue. If the call stack is empty, the event loop will take the first event from the queue and will push it to the call stack.

This event loop mechanism allows JavaScript to perform non-blocking I/O operations despite the fact that JavaScript is single-threaded.

The event loop has the following building blocks:

  • Call Stack. The Call Stack is a data structure in Javascript used to keep track of the function calls in a program. Call Stack follows LIFO principle, which means that the last function that gets pushed onto the stack is the first to be popped off when its execution completes.
  • Callback Queue. Callback queue is where callbacks from asynchronous operations are placed once they are ready to be executed.
  • Memory Heap. This is where memory allocation happens in JavaScript. When you create variables and objects in your JavaScript code, they are stored in the memory heap. This area of memory is not structured and is used for the dynamic allocation, meaning that variables and objects can be allocated and deallocated in any order.
  • Web API. The Web APIs are provided by the web browser that gives additional functionality to the V8 engine. The Web API calls are added to the Web API Container from the Call Stack. These Web API calls remain inside the Web API Container until an action is triggered.

This is a simplified version of Event Loop implementation:

while (true) {
  if (!isCallStackEmpty) {
    continue;
  }

  for (const task of eventLoop.microtasks) {
    task.execute();
  }
  eventLoop.microtasks = [];

  const task = eventLoop.nextMacrotask();
  if (task) {
    task.execute();
  }

  if (eventLoop.needRender()) {
    eventLoop.render();
  }
}

What happens when you type URL in a Browser

There are 6 general steps in this process:

  1. DNS lookup:

    • DNS lookup in Cache. The browser uses DNS lookup to get the IP address for the domain. Cached data at different levels makes the process faster.

    • Recursive DNS lookup in Server If the IP address cannot be found at any of the caches, the browser goes to DNS servers to do a recursive DNS lookup until the IP address is found

  2. TCP handshake. The browser establishes a TCP connection with the Server.

  3. TLS Negotiation. Determines which cipher will be used to encrypt the communication, verifies the Server, and establishes that a secure connection is in place before beginning the actual transfer of data

  4. Request to the Server. The Client sends an HTTP request to the Server.

  5. Server response to the Client. The Server processes the request and sends back the response for a successful response (the status code is 200).

  6. Rendering webpage. The browser renders the HTML content.

How does a browser render a webpage?

There are 6 general steps in this process:

  1. Construct DOM tree: a tree of nodes corresponding to HTML elements
  2. Construct CSSOM tree: a tree of nodes corresponding to CSS selectors
  3. Construct Render tree: The browser traverses the DOM tree and matches every element with the appropriate style from the CSSOM tree.
  4. Layout phase. geometry phase where browsers calculate the position of every element
  5. Painting phase. The browser uses a render tree to paint or display pixels currently within your viewport
  6. Repainting phase: if we change the DOM tree, the browser must repaint the affected regions

Difference between var, let, const

In JavaScript, var, let, and const are all used to declare variables, but they differ in their scope, hoisting, and reassignment capabilities. There are key differences:

  • Difference in scope:
    • var is function or globally scoped,
    • let and const are block-scoped
  • Difference in redeclaration and reassignment:
    • var allows redeclaration and reassignment.
    • let allows reassignment but not redeclaration in the same scope.
    • const does not allow reassignment or redeclaration.
  • Difference in hoisting:
    • Variables declared with var are hoisted and can be accessed before their declaration (with undefined value)
    • Variables declared with let and const are also hoisted to the top of their block, but they should not be initialized. Accessing them before the declaration results in a ReferenceError (Temporal Dead Zone).

JS Problems

Implement debounce

Debounce decorator is a high-order function that delays the function execution by the given timer. You are required to implement it.

Debouncing is particularly useful when you want to avoid the function being fired too frequently. A common use case is in search bars where you might want to wait for the user to finish typing before making an API call to fetch search results.

const debounce = (fn, ms) => {
   // ... your implementation
}

Example

const print = (n) => {
  console.log(n);
}

const fn = debounce(print, 1000);
fn(1); // ignore
fn(2); // ignore
fn(3); // prints 3

Solution

const debounce = (fn, ms) => {
  // timeoutID is used to store the ID of the setTimeout, allowing it to be cleared later.
  let timeoutID = null;
  
  // Returns a new function that has debouncing functionality.
  return (...args) => {
    // Clears the current timeout, if any, to reset the wait time for the function call.
    clearTimeout(timeoutID);    
    
    // Sets a new timeout. The original function fn will be called after the specified ms delay.
    timeoutID = setTimeout(() => {
      fn.call(this, ...args);
    }, ms);      
  }
} 

// Usage
window.addEventListener('resize', debounce(() => {
  // Will be printed once in a second
  console.log('Resizing...');
}, 1000));

Flatten the array

You are required to implement a custom Array.prototype.flat function. This function creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Example

// Input
const arr1 = [0, 1, 2, [3, 4]];

// Output
const res = flat(arr, 1) 
console.log(res) // [0, 1, 2, 3, 4]

Recursive solution

General idea:

  • Let's create a recursive function.
  • Let's write a base case for recursion: if depth is equal to 0, then just return given array
  • Iterate over a given array and on every step of iteration check whether the current item is an array or no
    • If it's an array, then flat it with the spread operator and call the recursive function with the current item
    • If it's not an array, then just push it to result array
  • Return result

Code

function flat(arr, depth = 1) {
  if (depth === 0) return arr;
  const result = []
  for (const item of arr) {
    if (Array.isArray(item)) {
      result.push(...flat(item, depth - 1)) 
    } else {
      result.push(item);
    }
  }
  
  return result;
}

const test = [1, 2, [3, 4, [5, 6], 7, 8], 9];
console.log(flat(test, 2)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
  • Time complexity: O(N)
  • Space complexity: O(N)

Implement forEach

You are required to implement Array.prototype.forEach method, which calls the given callback function for each element in a given array.

Example:

const arr = [1, 2, 3];
arr.forEach((item) => {
  console.log(item);
});
// Output: 1, 2, 3

Solution

Let's just iterate over a given array, and for every item, call the given callback function.

Array.prototype.myForEach = function(cb) {
  for (let i = 0; i < this.length; i++) {
    cb(this[i], i, this);
  }        
};

const arr = [1, 2, 3, 4, 5];
arr.myForEach( (item) => {
  console.log(item); // 1, 2, 3, 4, 5
});

Where:

  • cb is a callback function
  • this[i] is a current item of an array
  • this is a given array

Complexity:

  • Time complexity: O(N)
  • Space complexity: O(1)

Implement Array.prototype.map

You are required to implement a custom Array.prototype.map method, which is used to create a new array by applying a given callback function to every element of an original array. This method is particularly useful for transforming data.

Example:

const numbers = [1, 2, 3, 4, 5];

const squaredNums = numbers.map((number) => {
  return number * number;
});

console.log(squaredNums); // Output: [1, 4, 9, 16, 25]

Solution

  • Start by extending the Array.prototype to add the myMap function. This makes myMap available to all instances of arrays in your code.
  • Define the myMap function. The myMap function should take a callback function (cb) as its argument. This callback function will be applied to each element of the array.
  • Inside myMap, initialize an empty array named result. This array will store the results of applying the callback function to each array element.
  • Use a for loop to iterate over each element of the array (this). The this keyword refers to the array on which myMap is called.
    • In each iteration, apply the callback function to the current element, its index, and the entire array (cb(this[i], i, this)).
  • Push the result of the callback function into the result array.
  • After the loop completes, return the result array. This array contains the transformed elements.
Array.prototype.myMap = function(cb) {
  let result = [];
  for (let i = 0; i < this.length; i++) {
    result.push(cb(this[i], i, this));
  }
  return result;
}

// Usage
const arr = [1, 2, 3, 4, 5]
const res = arr.myMap(item => {
  return item*2;
})

console.log(res); // 1, 4, 6, 8, 10

Where:

  • cb is a callback function
  • this[i] is a current item of an array
  • this is a given array

Complexity:

  • Time complexity: O(N)
  • Space complexity: O(N)

Implement spy decorator

The Decorator is a design pattern that allows you to modify the existing functionality of a function by wrapping it in another function.

In the JavaScript testing framework Jest, a "spy" is typically used to monitor the behavior of a function:

spyOn(object, methodName)

You are required to implement Spy Decorator, which counts how many times the given function was called and with which arguments.

Example

const obj = {
   counter: 1, 
   add(num) {
      this.counter += num
   }
}

const spy = spyOn(obj, 'add')

obj.add(1) // 2
obj.add(2) // 4

console.log(obj.counter) // 4
console.log(spy.calls) // [ [1], [2] ]

Solution

  • Let's create a spyOn function which takes two arguments:
    • obj - the object containing the method to be spied on
    • methodName - a string representing the name of a method
  • Let's initialize an array calls, which is used to store the arguments with which the spied method is called.
  • Create originMethod which will be the reference to the original method from the object. This is necessary to maintain the original functionality of the method while adding the spy capability.
  • Ensure that the specified methodName corresponds to a function in the object. If not, an error is thrown. This is a safeguard to prevent incorrect usage of the spyOn function.
  • The original method on the object is then overridden with a new function. This new function does two things:
    • It records the arguments passed to the method call in the calls array.
    • It calls the original method using apply to ensure that the context (this) and arguments are passed correctly.
  • Finally, the spyOn function returns an object containing the calls array. This allows the user of the function to inspect the arguments with which the spied method has been called.

Code

function spyOn(obj, methodName) {
  const calls = [];

  const originMethod = obj[methodName];

  if (typeof originMethod !== "function") {
    throw new Error("not function");
  }

  obj[methodName] = function (...args) {
    calls.push(args);
    return originMethod.apply(this, args);
  };

  return {
    calls,
  };
}

Implement throttle

Throttling is a technique that ensures that the function does not execute more than once in a specified time period.

This is useful for managing the performance of functions that could be triggered frequently.

You are required to implement throttle(fn, delay) decorator function.

Example:

function throttle(fn, delay) {
  // Your implementation
}

const print = () => {
  console.log("print");
}

const fn = throttle(print, 1000);

// fn function will be called once in a second on window resize
window.addEventListener("resize", fn);

Solution

function throttle(fn, delay) {
  // Initialize a timer variable for managing the throttling
  let timer = null;

  // Return a new function that encapsulates the throttling logic
  return (...args) => {
    // if timer is already running, then just skip fn call
    if (timer) return;

    // Set a timer that delays the function execution
    timer = setTimeout(() => {
      // Execute the original function with the provided arguments
      fn(...args);
      
      // Clear the timer after the function executes and reset it to null
      clearTimeout(timer);
      timer = null;
    }, delay);
  };
}

const print = () => {
  console.log("print");
}

const fn = throttle(print, 1000);

// fn function will be called once in a second on window resize
window.addEventListener("resize", fn);

JS Components

Implement Autocomplete

You are required to Implement an Autocomplete component.

The autocomplete is a normal text input enhanced by a panel of suggested options.

This is initial HTML structure:

<div class="autocomplete">
  <input id="search" type="search" autocomplete="off">
  <ul></ul>
</div>

Requirements

  • Show suggestions when the user starts writing
  • By clicking on an option put it into the input
  • By clicking on the outside of the suggestions box, hide it

Solution

// Select input and unordered list elements from the DOM
const input = document.querySelector("#autocomplete input");
const ul = document.querySelector("#autocomplete ul");

// Sample data array for autocomplete suggestions
const data = ["apple", "banana", "cherry", "date"];

// Function to show suggestions based on the input
const showSuggestions = (arr) => {
  // Reduce direct DOM manipulation
  const listHTML = arr.map((item) => `<li class="list-item">${item}</li>`).join("");
  ul.innerHTML = listHTML;
  ul.style.display = listHTML ? "block" : "none";
}

// Function to handle click events on list items
const onItemClick = (evt) => {
  // Directly handle the click event on UL instead of individual LI
  if (evt.target.tagName === 'LI') {
    input.value = evt.target.textContent;
    ul.innerHTML = "";
  }
}

// Event listener for input changes
input.addEventListener("input", (evt) => {
  const query = evt.target.value.toLowerCase();
  const suggestions = query 
    ? data.filter(item => item.toLowerCase().startsWith(query))
    : [];
  showSuggestions(suggestions);
});

// Event listener for click events on the unordered list
ul.addEventListener("click", onItemClick);

// Event listener to close the suggestion list when clicking outside the autocomplete area
document.addEventListener("click", (event) => {
  if (!event.target.closest("#autocomplete")) {
    ul.innerHTML = "";
  }
});

Implement Tooltip

You are required to Implement an Tooltip component.

A tooltip is a floating, non-actionable informative label used to explain a UI element or feature. It's appears when users hover over, focus on, or tap an element.

This is initial structure of HTML:

<div data-tooltip="Name description">
  Hi, my name is John
</div>

Requirements:

  • Show tooltip on hover on element with attribute data-tooltip
  • Maintain multiple tooltips per page
  • Take content of the toolitp from data-tooltip attribute. It could be HTML
  • Optimisations
    • Show tooltip with some delay, e.g. 500ms

Solution

<style>
[data-tooltip] {
  text-decoration: underline;
  cursor: help;
}

[role="tooltip"] {
  opacity: 0;
  position: absolute;
  width: auto;
  height: auto;
}

[role="tooltip"].active {
  opacity: 1;
  transform: translateX(-50%); /* Center tooltip according parent element*/
}
</style>

<div>
  Many species of gecko have
  <span data-tooltip="Hi there!">adhesive</span> toe pads which enable
  them to climb walls and even windows.
</div>
const tooltips = document.querySelectorAll("[data-tooltip]");
const DELAY = 300;
let tooltipTimer = null;

const createTooltip = (target) => {
  let tooltip = document.createElement("div");

  tooltip.setAttribute("role", "tooltip");
  tooltip.setAttribute("inert", true);
  tooltip.textContent = target.dataset.tooltip;

  target.appendChild(tooltip);
};

const displayTooltip = (evt) => {
  const target = evt.target;
  const tooltip = target.querySelector("[role=tooltip]");
  
  // Get the target element's position and dimensions
  const { x, y, width, height } = target.getBoundingClientRect();
  
  // Set the tooltip position to be centered above the target element
  tooltip.style.left = `${Math.floor(x + width / 2)}px`;
  tooltip.style.top = `${Math.floor(y + height)}px`;

  tooltip.classList.add("active");
};

const hideTooltip = (evt) => {
  const tooltip = evt.target.querySelector("[role=tooltip]");
  tooltip.classList.remove("active");
};

tooltips.forEach((target) => {
  createTooltip(target);

  target.addEventListener("mouseenter", (e) => {
    clearTimeout(tooltipTimer);

    tooltipTimer = setTimeout(() => {
      displayTooltip(e);
    }, DELAY);
  });

  target.addEventListener("mouseleave", (e) => {
    clearTimeout(tooltipTimer);
    hideTooltip(e);
  });
});

Implement Star Rating component

You are required to Implement a Star rating component.

Solution

<div class="star-rating">
  <span class="star" data-value="1">&#9733;</span>
  <span class="star" data-value="2">&#9733;</span>
  <span class="star" data-value="3">&#9733;</span>
  <span class="star" data-value="4">&#9733;</span>
  <span class="star" data-value="5">&#9733;</span>
</div>
import "./styles.css";

const stars = document.querySelectorAll(".star");
let selectedValue = 0;

function handleStarClick(event) {
  selectedValue = event.target.getAttribute("data-value");

  // Remove "active" class from all stars
  stars.forEach((star) => {
    star.classList.remove("active");
  });

  // Add "active" class to selected stars and all previous stars
  for (let i = 0; i < selectedValue; i++) {
    stars[i].classList.add("active");
  }
}

function handleStarHover(event) {
  const hoveredValue = event.target.getAttribute("data-value");

  // Add "active" class to hovered stars and all previous stars
  for (let i = 0; i < hoveredValue; i++) {
    stars[i].classList.add("active");
  }

  // Remove "active" class from all stars after the hovered stars
  for (let i = hoveredValue; i < stars.length; i++) {
    stars[i].classList.remove("active");
  }
}

function handleMouseLeave() {
  // Remove "active" class from all stars after the last selected star
  for (let i = selectedValue; i < stars.length; i++) {
    stars[i].classList.remove("active");
  }

  // Add "active" class to selected stars and all previous stars
  for (let i = 0; i < selectedValue; i++) {
    stars[i].classList.add("active");
  }
}

// Add click and mouseenter event listeners to each star
stars.forEach((star) => {
  star.addEventListener("click", handleStarClick);
  star.addEventListener("mouseenter", handleStarHover);
  star.addEventListener("mouseleave", handleMouseLeave);
});

HTML & CSS

How to center a DIV element

How to center a DIV element horizontally and vertically? Name a different approaches.

Let's say that we have the following HTML structure:

<div class="parent">
  <div class="child">Centered Content</div>
</div>

Approach 1: Using Flexbox

If the parent container of your div is a flex container, you can center the div both horizontally and vertically with the following CSS:

.parent {
  display: flex;
  justify-content: center; /* align horizontal */
  align-items: center;     /* align vertical */
}

Approach 2: Using Grid

Similar to Flexbox, but with CSS Grid.

place-items is a shorthand for align-items and justify-items

.parent {
  display: grid;
  place-items: center;
}

Approach 3: Using Margin Auto

If you want to center a div horizontally, and it has a fixed width, you can use:

.child {
  margin: 0 auto; /* auto margin on left and right */
  width: 50%;    /* or any fixed width */
}

Approach 4: Using Absolute Positioning

.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Difference between px, rem, em units

There are two types of units in CSS:

  • Absolute units: px
  • Relative units: em, rem, %, vh

Absolute units don't change, while relative units are flexible and change based on context.

Let's dive into the most popular of them: px, em, and rem.

Pixels (px)

"Pixels (PX) are a common unit of measurement in web design. They are fixed and useful for maintaining consistent sizing of certain elements. However, they can pose problems for responsive sites."

Relative EM (em)

The em unit is relative to the font size of its closest parent element.

Root EM (rem)

REM is similar to EM, but always relative to the root element (HTML tag).

Relative units like %, EM, and REM are better for responsive design and accessibility standards.

Difference between <script>, <script async>, <script defer>

In HTML, the <script> tag is used to embed a JavaScript script in your webpage.

You can modify the behavior of this tag by using the async and defer attributes.

<script>

Script without any attributes is fetched and executed immediately, blocking the parsing of the rest of the HTML document until the script is loaded and executed.

<script async>

The script will be fetched in parallel to the HTML parsing and executed as soon as it’s downloaded. This does not block the HTML parsing.

It's often used for analytics or ad network-scripts.

<script defer>

The defer attribute fetches the script parallel to HTML parsing but delays its execution until after HTML parsing is complete. This ensures that the script and DOM work as intended.

Difference between reset.css and normalize.css

reset.css and normalize.css are CSS files that improve cross-browser consistency in HTML element styling, but they use different approaches.

reset.css

reset.css removes all built-in browser styling. Standard HTML elements like headings, paragraphs, and lists will appear unstyled and look the same in every browser, providing a clean slate for further styling.

This means you have to define all the styles for HTML elements explicitly.

normalize.css

normalize.css makes built-in browser styling consistent across different browsers but does not remove them entirely. It preserves useful default styles rather than "unstyling" everything.

Describe z-index, and it's stacking context

The z-index property controls the stacking order of elements on a web page.

An element with a greater stack order is always in front of an element with a lower stack order.

The z-index only works in combination with the positioned elements (position: absolute, position, relative, position: fixed, or position: sticky) and flex items.

Without a specified z-index, HTML elements are stacked based on their appearance order. Later elements appear on top of earlier ones if they overlap.

A stacking context forms when an element has a non-static position and a non-auto z-index. This limits z-index effects to a specific part of the layout.

Example

<div class="container">
  <div class="box box1">Box 1</div>
  <div class="box box2">Box 2</div>
  <div class="box box3">Box 3</div>
</div>

<style>
.container {
  position: relative; /* Establishes a stacking context */
}

.box {
  width: 100px;
  height: 100px;
  color: white;
  position: absolute; /* Needed for z-index to take effect */
  left: 50px; /* Overlap the boxes */
  top: 50px;
}

.box1 {
  background-color: red;
  z-index: 1; /* Higher z-index, appears on top */
}

.box2 {
  background-color: blue;
  z-index: 0; /* Lower z-index, appears behind box 1 */
  left: 100px; /* Slight horizontal shift */
  top: 100px;
}
</style>

Difference between cookie, sessionStorage, and localStorage

Cookie, sessionStorage, and localStorage are all methods used to store data on the client side, but they have different characteristics and use cases:

Cookies

  • Capacity: 4KB
  • Accessibility: All windows
  • Expiration: Manually set
  • Passed in HTTP request: Yes
  • Storage: Browser and Server
  • Use cases: session management, personalization, tracking, and analytics

Session Storage

  • Capacity: 5MB
  • Accessibility: Private for tab
  • Expiration: On tab close
  • Passed in HTTP request: No
  • Storage: Only browser
  • Use cases: similar to localStorage with a limitation of data expiration on tab close

LocalStorage

  • Capacity: 5/10 MB, depending on the browser
  • Accessibility: All windows
  • Expiration: Never
  • Passed in HTTP request: No
  • Storage: Browser only
  • Use cases: save user preferences and settings, cache date to reduce server requests, shopping cart state

Optimizations

Accessibility

Accessibility best practices:

  • Support keyboard navigation
  • Set focus on modals, popovers, alerts
  • Use correct HTML5 input types
  • Images should have alt descriptions
  • Use the lang attribute on the html element
  • Use only one h1 tag per page
  • Use a description with the title tag
  • Use aria landmarks where possible
  • Screen readers
  • Make sure that media is not autoplay
  • Provide transcriptions for video/audio
  • Add labels to form fields
  • Use text, not pictures of text
  • Web page size should not be larger than 500kb
  • Make a website for people with disabilities: larger font size, different color pallet
  • Dark/Light mode
  • Responsive design

Security

Security best practices:

  • Adding CSRF Token is a critical security measure for protecting web applications from unauthorized data submissions from other websites
  • Install SSL Certificate
  • Keep all libraries and frameworks up-to-date to avoid vulnerabilities found in older versions.
  • Limit User Permissions and Access
  • Always validate user input to protect against common vulnerabilities like SQL injection, cross-site scripting (XSS), and command injection. This should be done both on the client side for user experience and on the server side for security.
  • Define and enforce a strict CORS policy to prevent unauthorized domains from accessing your resources.
  • Use captcha
  • Utilize content security policies to prevent XSS attacks. Escape user input in HTML output to ensure that any input is rendered harmless.
  • Implement CSP headers to control the sources of various content types and mitigate XSS attacks.
  • Use X-Frame-Options or Content Security Policy (CSP) to prevent your website from being framed by malicious sites.
  • Use the Secure and HttpOnly flags for cookies to prevent access by client-side scripts and to ensure they're sent over HTTPS.
  • Set HTTP headers: "Referrer-Policy": "no-referrer"
  • Disable iframe embedding
  • Implement rate limiting and throttling to prevent brute-force attacks.
  • Regularly conduct security audits and penetration testing to identify and fix vulnerabilities.

Performance

  • Optimize Images: Compress and resize images to reduce file size without sacrificing quality. Use modern formats like WebP for better compression.

  • Minimize HTTP Requests: Reduce the number of requests by combining files, using CSS sprites, and minimizing the use of scripts and plugins.

  • Use a Content Delivery Network (CDN): CDNs distribute your content across multiple servers worldwide, reducing the distance between the server and the user, thus improving load times.

  • Enable Caching: Set appropriate caching policies to store frequently accessed resources on the user's device, reducing load times on subsequent visits.

  • Minify and Combine Files: Minify CSS, JavaScript, and HTML by removing unnecessary characters and spaces. Combine multiple files into one where possible to reduce the number of HTTP requests.

  • Use Asynchronous Loading for CSS and JavaScript: Load non-critical CSS and JavaScript files asynchronously to prevent them from blocking the rendering of the page.

  • Optimize CSS and JavaScript: Streamline and optimize code. Remove unused CSS and JavaScript to reduce file size.

  • Implement Lazy Loading: Load images and other resources only when they are needed (as the user scrolls down), which can significantly reduce initial load times.

  • Optimize Web Fonts: Limit the use of different font styles and weights, and use font-display settings to control how fonts are loaded.

  • Improve Server Response Time: Optimize your server, consider using a dedicated or virtual private server, and look into server-side caching.

  • Use Compression: Enable compression on your web server to reduce the size of your CSS, HTML, and JavaScript files.

  • Optimize Database Queries: For websites relying on a database, optimize queries and use indexing to speed up data retrieval.

  • Audit with Tools: Regularly use tools like Google PageSpeed Insights, Lighthouse, and GTmetrix to analyze and improve performance.

  • Mobile Optimization: Ensure your website is responsive and optimized for mobile devices, as more users are browsing on mobile.

  • Reduce Redirects: Each redirect triggers an additional HTTP request-response cycle, so minimize them to improve load times.

SEO

SEO (Search Engine Optimization) is a critical aspect of web development, as it helps to ensure that a website is accessible and ranks well on search engines like Google. Here's a list of best practices:

  1. Responsive Design: Ensure your website is mobile-friendly. Google prioritizes mobile-friendly websites in its search rankings.
  2. Page Speed Optimization: Faster loading times improve user experience and are favored by search engines. Optimize images, minify CSS and JavaScript, and leverage browser caching.
  3. Use of HTTPS: Secure your website with an SSL certificate. Google gives a ranking boost to secure websites.
  4. Optimized Content: Create high-quality, original content that includes relevant keywords. Avoid keyword stuffing and ensure that your content provides value to the user.
  5. Meta Tags and Descriptions: Use descriptive and keyword-rich meta tags and descriptions. They help search engines understand what your page is about.
  6. URL Structure: Use SEO-friendly URLs that are short, descriptive, and include keywords.
  7. Alt Text for Images: Provide descriptive alt text for images. This helps search engines understand the context of the images and makes your site more accessible.
  8. Internal Linking: Use internal links to guide users and search engines to relevant content on your site.
  9. External Linking: Link to authoritative and relevant external sites. This can lend credibility to your content.
  10. Schema Markup: Implement schema markup to provide search engines with more information about your content.
  11. Sitemap and Robots.txt: Use a sitemap to help search engines index your site and a robots.txt file to control what they index.
  12. Social Media Integration: Integrate social media sharing tools. Social signals are not a direct ranking factor but can help increase visibility and traffic.
  13. Regular Content Updates: Regularly update your website with fresh content. This signals to search engines that your site is active and relevant.
  14. User Experience (UX): Focus on creating a great user experience, including easy navigation and engaging design.
  15. Monitoring and Analytics: Use tools like Google Analytics and Google Search Console to monitor traffic, identify issues, and understand user behavior on your site.
  16. Local SEO: If your business is location-based, optimize for local search with local keywords, Google My Business listings, and local citations.
  17. Voice Search Optimization: Optimize for voice search by including conversational keywords and providing clear, concise answers to common questions.
  18. Accessibility: Ensure your site is accessible to all users, including those with disabilities. This can involve using proper contrast, keyboard navigation, and screen reader-friendly design.

About

Frontend Interview Cheatsheet. Short summarized answers to the most popular Interview Questions. Daily updates in Telegram Channel: https://t.me/zenfrontend

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages