JavaScript Basics Interview Questions and Answers
Build a foundation in JavaScript's role on the web, core terminology, and the ideas interviewers expect every candidate to explain.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
A programming language is your way of talking to a computer. You write instructions in a specific syntax, and the computer follows them. Each language has its own strengths - Python is great for data work, JavaScript dominates the web, and C++ is the go-to for performance-heavy stuff like games. The key thing interviewers want to hear is that you understand why different languages exist for different jobs.
Front-end is the part delivered to the browser and used through the interface. It includes the HTML structure, CSS styling, and JavaScript behavior behind buttons, forms, and other interactive elements. A browser may also run background code, such as a service worker, that users do not see directly.
Back-end is the behind-the-scenes part users never see. It handles the server, the database, and the business logic that makes an app actually work. When you log in, your credentials get verified on the back-end. When you post a comment, the back-end saves it. It powers everything on the front-end.
HTML (Hypertext Markup Language) gives web pages their structure. Think of it as the skeleton of every website. You use tags to tell the browser "this is a heading," "this is a paragraph," "this is an image." Without HTML, a browser wouldn't know how to organize content on the screen.
CSS (Cascading Style Sheets) controls how HTML elements look. Colors, fonts, spacing, layouts, animations - that's all CSS. HTML builds the structure, and CSS makes it look good. The "cascading" part means styles can override each other based on specificity, which is a common gotcha for beginners.
JavaScript is a programming language commonly used to add behavior to web pages. It can update the UI, respond to events, and communicate with servers.
What makes it special is that with Node.js, you can also run JavaScript on the server. This lets teams use one language across parts of an application, although the browser and server still provide different APIs and responsibilities.
const button = document.querySelector("#save");
button.addEventListener("click", async () => {
const response = await fetch("/api/save", { method: "POST" });
console.log(response.ok ? "Saved" : "Save failed");
});
This shows browser JavaScript responding to an event and communicating with a server API.
| JavaScript | Java |
|---|---|
| Commonly used for front-end web development and browser-based apps | Used for enterprise server-side apps and Android development |
| Dynamically typed - no type declarations needed | Statically typed - types declared at compile time |
| Executed by engines that parse, compile, and often JIT-optimize code | Compiled to bytecode, then runs on the JVM |
| JavaScript execution on a given agent is generally single-threaded; async APIs and workers support concurrency | Natively supports multi-threading |
| Runs anywhere there's a browser or Node.js | Runs on any platform with a JVM installed |
| Powers front-end frameworks, Node.js servers, full-stack apps | Enterprise backends, Android apps, big data pipelines |
| Huge ecosystem: React, Vue, Angular, Express | Mature ecosystem: Spring Boot, Hibernate, Maven |
Yes, JavaScript is case-sensitive, and this trips up
beginners constantly. myVariable and
myvariable are two completely different
identifiers. This applies to variable names, function
names, and even built-in keywords. Writing
getElementById as getElementByID
will throw an error. Always double-check your casing.
let myVariable = "correct casing";
let myvariable = "different identifier";
console.log(myVariable); // correct casing
console.log(myvariable); // different identifier
// console.log(myVaraible); // ReferenceError: typo
Think of building a house: HTML is the walls and rooms (structure), CSS is the paint and furniture (styling), and JavaScript is the electricity and plumbing (behavior). You need all three working together. HTML alone gives you a plain document. Add CSS and it looks great. Add JavaScript and it becomes interactive. Read more
In a single-threaded environment, JavaScript runs one job at a time on a given call stack. Two callbacks do not mutate shared state simultaneously on that stack, but asynchronous callbacks can still observe stale state or update it in an unexpected order. Workers can also introduce true cross-thread coordination problems. A long synchronous computation can freeze the page, so use asynchronous APIs for waiting and Web Workers for CPU-heavy work.
'Data' is the broad term for any information your
program works with - user input, API responses,
config settings, whatever. A 'value' is one specific
piece of that data at a particular moment. For
example, a user's profile is data. The string
"John" stored in user.name is a value.
Data is the concept, values are the concrete things
your code actually manipulates.
You'll notice you never write "use strict" at the top
of your React or Angular files. That's because ES6
modules automatically run in strict mode. Since both
frameworks use import/export
syntax everywhere, strict mode is already on by
default. Adding it manually would just be redundant.
This is a good interview detail - it shows you
understand how modules work under the hood.
High-level languages like JavaScript and Python hide hardware details and let you focus on solving problems quickly. But sometimes you need direct control over memory and CPU.
Low-level languages like C and Assembly shine in embedded systems (think microcontrollers in your car), operating system kernels, device drivers, and anywhere performance is critical - like game engines or real-time audio processing. When every microsecond counts, you want low-level control.
High-level languages
(JavaScript, Python, Ruby) let you write code that
reads almost like English. You don't worry about
memory allocation or CPU registers. Trade-off?
Slightly less performance.
Low-level languages
(C, Assembly) give you direct control over hardware
and memory. The code is harder to write and read, but
you get maximum performance and minimal overhead. The
quick test: if the language manages memory for you,
it's high-level. If you're calling malloc
yourself, it's low-level.
Dealing with JavaScript compatibility is a daily reality for web developers. Here's your toolkit:
Polyfills and transpilers solve two different problems when it comes to JavaScript compatibility :
- Polyfills:
- Transpilers (e.g., Babel):
JavaScript doesn't force you into one coding style. You can use multiple programming paradigms depending on what fits your problem. Need classes and inheritance? Go object-oriented. Want pure functions and immutability? Use functional programming. Quick script that runs top-to-bottom? Procedural works fine. Most real-world JavaScript mixes these styles. React, for example, leans heavily into functional patterns with hooks, while Angular uses OOP-style classes.
Reserved keywords
are words that JavaScript has already claimed for its own grammar - things like if, return, class, let, and const. You can't use them as variable or function names because the engine would get confused about whether you mean the keyword or your variable. A common trip-up: class is reserved even in contexts where you're not using classes, and some words like await are only reserved inside async functions.
Arithmetic operators
are your basic math toolkit: addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation (**). The one that trips people up is +, because JavaScript overloads it for string concatenation too. So 5 + "3" gives you "53", not 8. Also worth knowing: modulus (%) is handy for checking even/odd numbers or cycling through arrays.
= (Assignment Operator): This puts a value into a variable. x = 5; stores 5 in x. It's not asking a question - it's giving an order.
== (Equality Operator): This asks "are these loosely equal?" and lets JavaScript convert types to make it work. So 5 == '5' returns true because JS converts the string to a number first. This is where bugs love to hide.
=== (Strict Equality Operator): This asks "are these exactly the same value AND type?" No conversions, no surprises. 5 === '5' returns false because number and string are different types. This is the one you should use by default. A common interview test: mixing these up in conditions is one of the most frequent JavaScript bugs in code reviews.
- Capabilities:
- Limitations:
Forward compatibility means older code can handle data or content from a newer version without breaking. It's harder to guarantee than backward compatibility since you can't predict future formats - the old system has to be designed to gracefully ignore things it doesn't understand rather than crashing.
Backward compatibility means new versions of a system can still work with older data and code. JavaScript takes this extremely seriously - the spec almost never removes features because breaking existing websites is unacceptable. That's why you still see quirky old behaviors (like typeof null === "object") that can't be fixed without breaking the web.
ISO/IEC 22275 is based on ECMA-262 - it's essentially the same JavaScript spec published as an ISO standard. This matters mainly for organizations or governments that require ISO-certified standards for procurement. In day-to-day development you won't encounter it, but it's worth knowing it exists.
Putting "use strict" at the top of a file or function enables strict mode, which catches common mistakes that would otherwise fail silently - like accidentally creating global variables by omitting var, or assigning to read-only properties. ES modules are always in strict mode by default, so in modern code you'll rarely write it explicitly, but knowing what it does is useful when debugging older codebases.
In the browser you can use inline <script> tags or link external .js files - external files are almost always the right choice for maintainability. Beyond the browser, JavaScript runs on servers with Node.js, in mobile apps via React Native, in desktop apps via Electron, and in serverless functions like AWS Lambda or Cloudflare Workers. The browser console is also great for quick experiments.
A compiler reads your entire source code upfront and translates it into machine code or bytecode before any of it runs - think C or Java. The compiled output runs fast since the heavy lifting is done upfront. The downside is you need a build step, and errors only surface at compile time rather than interactively as you run the code.
An interpreter executes translated instructions as the program runs. Compilation and interpretation are strategies that can be combined, not fixed labels for whole languages. Modern JavaScript engines parse source, begin execution, and may use JIT compilation to optimize frequently executed code. This is why the old claim that JavaScript is simply "interpreted" does not accurately describe current engines.
A compiler translates source into another executable representation before or during execution. An interpreter executes translated instructions as it goes. Modern JavaScript engines can begin executing quickly, then JIT-compile and optimize hot paths. They may later discard an optimization if its runtime assumptions change, so "JavaScript is both" is a useful shorthand, not a complete description of every engine.
The language defines syntax and core behavior such as functions, objects, and promises. A host environment supplies APIs around it: browsers provide the DOM, while Node.js provides modules and file-system APIs. Code that calls a host API depends on that environment even though the language itself is JavaScript.
JavaScript runs one piece of synchronous code at a time on its main execution thread. The host can perform work such as network I/O separately and schedule callbacks; the event loop runs those callbacks when the current stack is clear. Long synchronous work still blocks that thread.
JavaScript's core language is shared, but each host exposes different APIs and globals. Browser code can use document and window, while Node.js code can use server-side APIs such as the file system; code using one host's APIs needs adaptation to run in the other.
JavaScript Practical and Coding Interview Questions
Problem: Store a learner's name and score in variables, then print both values in one summary.
const learner = { name: 'Ada', score: 9 };
console.log(learner.name + ' scored ' + learner.score);