Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
JavaScript for Frontend Interviews
  1. 1.Introduction to JavaScript & Setup
  2. 2.Variables and Data Types
  3. 3.Operators and Expressions
  4. 4.Control Flow: Conditionals
  5. 5.Control Flow: Loops
  6. 6.Functions: Basics
  7. 7.Arrays and Objects
  8. 8.Higher-Order Functions & Callbacks
  9. 9.Scope and Closures
  10. 10.`this` Keyword and Context
  11. 11.Asynchronous JavaScript: Callbacks & Promises
  12. 12.ES6+ Features (Part 1)
  13. 13.ES6+ Features (Part 2)
  14. 14.Error Handling & Debugging
  15. 15.Asynchronous JavaScript: Async/Await
  16. 16.Prototypal Inheritance & `new` Keyword
  17. 17.Event Loop, Microtasks & Macrotasks
  18. 18.Working with the DOM
  19. 19.Advanced Array Methods & Data Structures
  20. 20.Frontend Interview Prep & Best Practices
JavaScript for Frontend Interviews
01

JavaScript · Lesson 1 of 20 · 4 min

Introduction to JavaScript & Setup

This lesson is about the environment where your code runs, and how to explain the difference between browser and Node.js contexts in an interview.

You already know what JavaScript is. This isn't about console.log("Hello, World!"). It's about the runtime environments—the browser and Node.js—and the subtle differences that interviewers use to test your understanding of the fundamentals.

Getting this right shows you understand that JavaScript doesn't run in a vacuum. It's hosted, and the host provides the APIs you use to interact with the outside world.

How JavaScript Executes

You run JavaScript in three main contexts. Each has a purpose.

  1. The Browser Console: For quick, ephemeral tests. You have access to the window object and the DOM of the current page. It's a live playground, not a development environment.

  2. A Node.js Process: For running standalone scripts, build tools, or servers. You execute a file from your terminal with node your-script.js. There is no DOM, no window, and no browser APIs.

  3. An HTML <script> Tag: The standard way to run JS on a web page. The browser parses the HTML, finds the <script> tag, and then fetches and executes the code. This is the primary context for frontend development.

Browser vs. Node: The Core Differences

This is a classic interview question. The V8 engine might be the same, but the runtime environments provide different global objects and APIs. Knowing the distinction is non-negotiable.

| Feature | Browser | Node.js | | :--- | :--- | :--- | | Global Object | window | global | | Purpose | Interactive web pages | Server-side logic, scripting | | DOM Access | Yes (document, etc.) | No | | File System | No (sandboxed) | Yes (via fs module) | | APIs | fetch, localStorage, setTimeout | http, path, process |

The trap: Candidates often forget that APIs like fetch or localStorage are provided by the browser, not the JavaScript language itself. While Node.js has added fetch recently, the principle holds: the environment provides the I/O.

An interviewer might ask: "You have a piece of utility code that formats dates. Can you use it on both the frontend and the backend? What might prevent it from working?"

The answer they're looking for is about dependencies. If your code calls window.location or document.getElementById, it's tied to the browser. If it relies on the fs module to read a file, it's tied to Node.js. Pure, dependency-free JavaScript is portable.

How Scripts Load in the Browser

How and where you load your script impacts page performance. Placing a <script> tag in the HTML can block rendering, making your page appear slow.

Without any attributes, a <script> tag tells the browser to:

  1. Stop parsing HTML.
  2. Fetch the script file.
  3. Execute the script.
  4. Resume parsing HTML.

This is why a large script in the <head> is a performance killer. The user sees a blank page until the script is downloaded and run.

To avoid this, you have two attributes: async and defer. Both allow the browser to download the script without blocking the HTML parser.

  • defer: Downloads the script in parallel and executes it only after the HTML parsing is complete, but before the DOMContentLoaded event. Scripts with defer execute in the order they appear in the HTML. This is the safest and most common choice for scripts that need the full DOM.

  • async: Downloads the script in parallel and executes it as soon as it's available. This can happen before or after HTML parsing is finished. It does not guarantee order. Use this for independent, third-party scripts like analytics, where execution order doesn't matter.

The old advice: "Put your scripts at the bottom of the <body>." This works, but <script defer> in the <head> is generally better. It allows the browser to discover and start downloading the script earlier, without blocking rendering.

Here is the most common and robust pattern for including JavaScript.

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>External JS Example</title>
    <!-- Using defer lets the browser download the script early without blocking the page -->
    <script src="script.js" defer></script>
</head>
<body>
    <h1>External JavaScript Demo</h1>
    <p id="message">This text will be changed by JavaScript.</p>
</body>
</html>

script.js:

// This code runs after the HTML is fully parsed because of the 'defer' attribute.
console.log("Hello from external JavaScript!");

// Access an HTML element by its ID and change its text content.
// This is safe with 'defer' because the element is guaranteed to exist.
const paragraph = document.getElementById('message');
if (paragraph) {
    paragraph.textContent = "JavaScript successfully changed this message!";
    paragraph.style.color = 'green';
}

Try it

Create an index.html file with a single <p> tag. Create a script.js file that tries to change the text of that paragraph. First, load the script in the <head> with no attributes and observe the error in the console. Then, add the defer attribute and confirm it works as expected. This takes 15 minutes and solidifies the concept of script execution order.

Next: We will cover the foundational building blocks of the language in Variables and Data Types.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Next

Variables and Data Types