Basic JavaScript Interview Questions
JavaScript Basics: Basic JS Questions
What is JavaScript?
View Answer:
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.
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:
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:
{' '}
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:
{' '}
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:
Criteria | JavaScript (Interpreted) | C++ (Compiled) |
---|---|---|
Compilation | No 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 Speed | Generally 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. |
Debugging | Easier to debug, errors appear at runtime, line by line. | Debugging can be more complex. Errors not caught at compile time may cause unpredictable behaviors. |
Portability | Highly 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 Case | Commonly 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:
Why is it called JavaScript?
View Answer:
On which platforms can we implement JavaScript?
View Answer:
Why is it good to remember code names for different JavaScript Engines?
View Answer:
{' '}
Here's a table of some commonly used JavaScript engines and their code names:
JavaScript Engine | Code Name |
---|---|
Google Chrome | V8 |
Firefox | SpiderMonkey |
Safari | JavaScriptCore (Nitro) |
Edge (pre-Chromium) | Chakra |
Node.js | V8 |
Opera | Carakan (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:
In-browser JavaScript can accomplish everything related to webpage alteration, user interaction, and webserver interaction.
For instance, in-browser JavaScript can:
- Modify the existing text, add HTML, and design the page.
- Respond to user activities, such as mouse clicks, pointer movements, and keystrokes.
- Send network requests to distant servers and download and upload files (so-called AJAX and COMET technologies).
- Get and set cookies, ask the visitor questions, and display messages
- Track client-side data ("local storage").
What CAN'T in-browser JavaScript do and why?
View Answer:
- 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.
- 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.
- 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.
- 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).
- 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:
Can you name some modern alternative languages that convert to JavaScript?
View Answer:
- 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.
- TypeScript concentrates on adding "strict data typing" to simplify the development and support of complex systems, and Microsoft develops it.
- Flow also adds data typing, but differently, and Facebook developed it.
- Dart is a standalone language with an engine that runs in non-browser environments (like mobile apps) and converts to JavaScript—developed by Google.
- Brython is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
- Kotlin is a modern, concise and safe programming language that can target the browser or Node.
{' '}
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:
{' '}
let test;
console.log(test); // Outputs: undefined
test = null;
console.log(test); // Outputs: null