JavaScript Output Interview Questions and Answers
Review browser and console output methods, including when to use them and their practical limitations.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What are the different ways of writing output in JavaScript?
- Difference between console.log and console.table?
- What is the difference between returning and logging a value?
- What does console.log do?
- How can a template literal include a variable?
- Why might console.log of an object look different later in devtools?
- Why is logging a mutable object not always a reliable snapshot?
Theoretical JavaScript Interview Questions
For debugging, console.log() is your primary tool - also check out console.table() for objects and console.error() for errors. For the UI, set element.textContent or element.innerHTML to update page content. Avoid document.write() in modern code - it overwrites the entire page if called after load. Reserve alert() for quick debugging only; never ship it to users.
console.log() dumps a raw representation of any value - great for quick inspection. console.table() takes an array of objects and renders a proper table with column headers, making it dramatically easier to compare rows of structured data. When debugging a list of users or orders, console.table(data) is far more readable than scrolling through nested log output.
It writes values to the developer console, which is useful for inspecting program output during development; it does not display a browser dialog.
return supplies a function result to its caller, while console.log reports a value to the developer console. Logging alone does not make a function return that value.
Place the expression inside ${...} within backticks; JavaScript evaluates it and inserts its string representation.
It presents arrays of records or objects in a tabular console view, making columns and repeated fields easier to compare.
Use Intl.NumberFormat to produce a locale-aware string for presentation, and retain the original Number for calculations.
Some consoles display expandable object references whose properties may be inspected after logging. Log a deliberate snapshot, such as JSON text for suitable plain data, when timing matters.
Use it to distinguish diagnostic errors from ordinary output in development tooling; avoid exposing sensitive user data in logs.
It accepts multiple values and displays them together, which can preserve useful object and number representations better than concatenating everything into one string.
JavaScript evaluates function arguments from left to right before calling the function, so side effects in arguments occur in that order.
Promise callbacks run as microtasks after the current synchronous stack completes, so a log scheduled by then() may follow a later direct log.
A console may retain a reference and show its later state when expanded. Serialize or copy the relevant fields at log time when an immutable snapshot is needed.
Do not log credentials, access tokens, sensitive personal data, or confidential payloads. Redact fields and limit diagnostic output to what is needed.
Assert on meaningful returned values or structured behavior rather than environment-specific console formatting, unrelated async ordering, or exact object-inspector presentation.