Skip to main content

Basic JavaScript Interview Questions

JavaScript Basics: Basic JS Questions




What is JavaScript?

View Answer:
Interview Response: JavaScript is a high-level, interpreted programming language primarily used for enhancing interactivity and providing rich web content in browser-based applications. It supports event-driven, functional, and imperative programming styles.

Here is a simple JavaScript code example:

function sayHello(name) {
return 'Hello, ' + name + '!';
}

console.log(sayHello('JavaScript')); // Prints: Hello, JavaScript!

This code defines a function that concatenates a string with an input name, and logs the output to the console.


note

It should be noted, to maintain efficient speed in the browser, V8 translates JavaScript code into more efficient machine code instead of using an interpreter. During execution, it compiles JavaScript code into machine code using a JIT (Just-In-Time) compiler, much like SpiderMonkey or Rhino in the Mozilla browser.


What are JavaScript's main features?

View Answer:
Interview Response: JavaScript's main features include dynamic typing, object-oriented programming, first-class functions, closures, asynchronous programming (promises, async/await), event-driven interaction, and integration with web technologies like HTML and CSS.

Here's an example illustrating a few key features of JavaScript, including dynamic typing, object-oriented programming, and asynchronous programming:

// Object-oriented programming
let student = {
firstName: 'John',
lastName: 'Doe',
age: 20,
getFullName: function () {
return this.firstName + ' ' + this.lastName;
},
};

// Dynamic typing
let variable = 'Hello, ';
variable = variable + student.getFullName();
console.log(variable); // Prints: Hello, John Doe

// Asynchronous programming
setTimeout(function () {
console.log('This is printed after 2 seconds');
}, 2000);

In this code, student is an object with properties and a method. We demonstrate dynamic typing by changing variable from a string to another string. The setTimeout function shows a simple example of asynchronous behavior.


What is the difference between a high and low-level programming language?

View Answer:
Interview Response: A high-level programming language is designed to be more abstract, and closer to natural language, making it easier for programmers to read, write, and maintain code. Low-level programming languages are closer to machine language, and provide greater control over hardware, but require more effort and knowledge to program effectively.

Code Example:

{' '}

Here is an example of high-level programming language code (JavaScript):

let a = 10;
let b = 20;
let sum = a + b;
console.log(sum); // prints 30

And here's a comparative example in a low-level language (Assembly, specifically x86 assembly):

section .data
a db 10
b db 20
sum db 0

section .text
global _start

_start:
mov al, [a]
add al, [b]
mov [sum], al

; Print sum
mov eax, 4
mov ebx, 1
mov ecx, sum
mov edx, 1
int 0x80

; Exit
mov eax, 1
xor ebx, ebx
int 0x80

In this assembly code, we manually load the values of a and b into a register, add them, and store the result back into sum. Then we call an interrupt to print sum and another to exit the program. This level of detail and manual control is characteristic of low-level languages.


Is JavaScript an interpreted or compiled programming language?

View Answer:
Interview Response: JavaScript is traditionally considered an interpreted language, as it's executed line by line in a browser. However, modern engines use Just-In-Time (JIT) compilation for improved performance, blurring the line between interpreted and compiled.

Technical Details: In a compiled language, the target machine directly translates the program. The target machine does not translate the source code directly into an interpreted language. Instead, a different program, an interpreter, reads and executes the code. In simple terms: JavaScript is an interpreted language.

Table: Interpreted vs Compiled

{' '}

Here's a simple table of differences between Interpreted and Compiled languages using JavaScript as an example of an interpreted language and C++ as an example of a compiled language:

CriteriaJavaScript (Interpreted)C++ (Compiled)
CompilationNo separate compilation step. Code is typically parsed and executed line-by-line by a JavaScript engine using JIT compilation.Requires a separate compilation step before running, where the source code is translated to machine code.
Execution SpeedGenerally slower, due to the overhead of interpreting code or JIT compiling at runtime.Generally faster, as the code is already compiled to machine code before execution.
DebuggingEasier to debug, errors appear at runtime, line by line.Debugging can be more complex. Errors not caught at compile time may cause unpredictable behaviors.
PortabilityHighly portable as long as the host system has a JavaScript engine (like a web browser).Compiled binaries are platform-dependent and may require recompiling for different systems.
Use CaseCommonly used for web development, enhancing interactivity in web pages.Used for system software, game development, and where performance is critical.

What is the name of the JavaScript scripting language specification called?

View Answer:
Interview Response: The name of the JavaScript scripting language specification is called ECMAScript, which is maintained by the ECMA International Standards organization.

Why is it called JavaScript?

View Answer:
Interview Response: In 1995, JavaScript was created by Brendan Eich at Netscape Communications Corporation, originally named Mocha and then Live Script, before being renamed to JavaScript. The name was chosen to capitalize on Java's popularity and attract Java developers to the web. Despite its name, JavaScript is a distinct language with its own unique syntax and features.

On which platforms can we implement JavaScript?

View Answer:
Interview Response: JavaScript works in any environment that has a JS engine.

Technical Response: JavaScript is a flexible language that can run on a wide range of platforms, as long as they support a JavaScript engine, such as web browsers, servers, desktop applications, and IoT devices.

Why is it good to remember code names for different JavaScript Engines?

View Answer:
Interview Response: It is good to remember the names of engines to ensure features work in all environments. If not, we must write a polyfill.

Code Names:

{' '}

Here's a table of some commonly used JavaScript engines and their code names:

JavaScript EngineCode Name
Google ChromeV8
FirefoxSpiderMonkey
SafariJavaScriptCore (Nitro)
Edge (pre-Chromium)Chakra
Node.jsV8
OperaCarakan (pre-2013), V8 (post-2013)

These engines are used to parse and execute JavaScript code in their respective environments.


What can in-browser JavaScript do?

View Answer:
Interview Response: In-browser JavaScript can manipulate the Document Object Model, respond to user events, make HTTP requests, and store data in cookies or local storage. It can also create animations and validate forms.

Technical Response: JavaScript's capabilities get heavily influenced by the environment in which it runs. Node.js, for example, includes methods that allow JavaScript to read/write arbitrary files and make network requests.

In-browser JavaScript can accomplish everything related to webpage alteration, user interaction, and webserver interaction.

For instance, in-browser JavaScript can:
  1. Modify the existing text, add HTML, and design the page.
  2. Respond to user activities, such as mouse clicks, pointer movements, and keystrokes.
  3. Send network requests to distant servers and download and upload files (so-called AJAX and COMET technologies).
  4. Get and set cookies, ask the visitor questions, and display messages
  5. Track client-side data ("local storage").

What CAN'T in-browser JavaScript do and why?

View Answer:
Interview Response: In-browser JavaScript can't access files or data on the user's computer, for security reasons. It also can't perform certain network requests, due to CORS policies. The aim is to prevent a malicious website from accessing users' data or harming them.

Technical Response: JavaScript's capabilities in the browser are limited to safeguard the user's safety. The purpose is to prevent a malicious website from acquiring private information or inflicting data damage to users.

Examples of such constraints include:

  1. JavaScript permits us to read/write files directly on the hard disk, copy them, or run applications on a web page, however, it does not have direct access to OS functionality.
  2. Modern browsers allow it to interact with files. Still, access is limited and only provided if the user performs specific actions, such as "dropping" a file into a browser window or choosing it through a tag.
  3. Interacting with the camera/microphone and other devices is possible, but it requires the user's explicit consent. The JavaScript-enabled page may not secretly activate a web camera, examine its surroundings, and communicate the data.
  4. JavaScript from one page may not be able to access JavaScript from another if they are from separate sites (from a different domain, protocol, or port).
  5. JavaScript can easily connect with the server that serves the current page through the internet. However, its capacity to receive data from other sites/domains is severely limited. Although feasible, it requires explicit agreement from the remote side (represented in HTTP headers).

What makes JavaScript unique?

View Answer:
Interview Response: JavaScript is unique because it fully integrates with HTML and CSS, and all major browsers support it. JavaScript is the only browser technology that combines, all three of these features. That distinguishes JavaScript, and explains why it is the most widely used technology for designing browser interfaces.

Can you name some modern alternative languages that convert to JavaScript?

View Answer:
Interview Response: Some of the alternatives to JavaScript include Coffee Script, TypeScript, Flow, Bry-thon, Dart, and Kotlin.

Technical Response: Several popular languages are trans-piled (converted) to JavaScript before running in the browser.

Examples of such languages:

  1. CoffeeScript is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write more transparent and more precise code—usually, Ruby devs like it.
  2. TypeScript concentrates on adding "strict data typing" to simplify the development and support of complex systems, and Microsoft develops it.
  3. Flow also adds data typing, but differently, and Facebook developed it.
  4. Dart is a standalone language with an engine that runs in non-browser environments (like mobile apps) and converts to JavaScript—developed by Google.
  5. Brython is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
  6. Kotlin is a modern, concise and safe programming language that can target the browser or Node.

Code Example: TypeScript

{' '}

TypeScript is a strongly typed, object-oriented, compiled language. It is a super-set of JavaScript. TypeScript adds optional types, classes, and modules to JavaScript.

class Greeter {
greeting: string;

constructor(message: string) {
this.greeting = message;
}

greet() {
return 'Hello, ' + this.greeting;
}
}

let greeter = new Greeter('JavaScript!');
console.log(greeter.greet()); // Outputs: Hello, JavaScript!

In this example, the Greeter class has a property greeting of type string. The constructor method is a special method for creating and initializing an object created with a class. This method accepts one parameter message of type string.

The greet method returns a string that includes the greeting property.

The let greeter = new Greeter("JavaScript!"); line creates a new Greeter object with the greeting property set to "World".

Finally, console.log(greeter.greet()); calls the greet method on the greeter object and logs the return value to the console.


What is the difference between "undefined" and "null" in JavaScript?

View Answer:
Interview Response: "Undefined" means a variable has been declared but not assigned a value. "Null" is an assignment value that means no value or no object. It implies absence of value.

Code Example:

{' '}

let test;
console.log(test); // Outputs: undefined

test = null;
console.log(test); // Outputs: null