DEV Community

Cover image for Common JavaScript Questions and Answers.
Valentina Onwuli
Valentina Onwuli

Posted on • Updated on

Common JavaScript Questions and Answers.

First, I want you to know that JavaScript is not Java. They are two very different languages. JavaScript is not a subset of Java. It is not interpreted Java. (Java is interpreted Java!). So here are a few questions and answers on JavaScript below;

1. What are the different data types present in JavaScript?
There are two kinds of data types:
1) Primitive Data Types
2) Non Primitive Data Type

1) Primitive Data Types:

a. Strings: JavaScript's string type is used to represent textual data. Strings are written with quotes, you can use single or double quotes. Each element in the string occupies a position in the string. The first element is at index 0, the next at index 1, and so on. The length of a string is the number of elements in it. Example:

let fruitName1 = "Apple";  // Using double quotes
let fruitName2 = 'Orange'; // Using single quotes
Enter fullscreen mode Exit fullscreen mode

b. Numbers: The Number type is a double-precision 64-bit binary format IEEE 754 value. It is capable of storing floating-point numbers between 2^-1074 and 2^1024, but can only safely store integers in the range -(2^53 − 1) to 2^53 − 1. Values outside of the range from Number.MIN_VALUE to Number.MAX_VALUE are automatically converted to either +Infinity or -Infinity. Example:

let x = 68.00;     // Written with decimals
let x2 = 657;        // Written without decimals
Enter fullscreen mode Exit fullscreen mode

c. Boolean: represents a logical entity and can have two values: true and false.

let x = 25;
let y = 25;
let z = 98;

(x == y)       // Returns true
(x == z)       // Returns false
Enter fullscreen mode Exit fullscreen mode

d. Undefined: a global variable that has not been assigned a value.

let foo;
console.log('is undefined?', foo === undefined);
Enter fullscreen mode Exit fullscreen mode

e. Null: represents the intentional absence of any object value. Null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object.

2)Non Primitive Data Type:

a. Object: In JavaScript, objects can be seen as a collection of properties. It is also a referenceable container of name/value pairs. The names are strings (or other elements such as numbers that are converted to strings). The values can be any of the data types, including other objects.

An object begins with {left brace and ends with }right brace. Each name is followed by :colon and the name/value pairs are separated by ,comma. Example:

const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Enter fullscreen mode Exit fullscreen mode

2) Explain Hoisting in JavaScript.

JavaScript Hoisting refers to the process whereby the declaration of functions, variables or classes is moved to the top of their scope, prior to execution of the code.
Hoisting allows functions, variables or class to be declared before initialized. Example:

var foo; // → Hoisted variable declaration
console.log(foo); // → undefined
foo = 42; // → variable assignment remains in the same place
console.log(foo); // → 42
Enter fullscreen mode Exit fullscreen mode

3) Difference between “==” and “===” operators.

“==” and “===” are both comparison operators but they have a difference; the equality operator (==) checks whether its two operands are equal, returning a Boolean value. It tries to transform and compare operands which can be of various types but the strict equality operator (===) checks if its two operands are equal, returning a Boolean result. The strict equality operator always considers operands of different types to be different.

In summary, “==” convert their operands if the operand types do not match but “===” does not convert operands, it ensures that they belong to the same type and are the same value.

4) Explain Implicit Type Coercion in JavaScript.

Implicit coersion simply refers to Javascript trying to coerce values from one data type to another (think of JavaScript automatically converting a string to a number).

5) Is JavaScript a statically typed or a dynamically typed language?

JavaScript is a dynamically typed language.

6) What is NaN property in JavaScript?

NaN is a variable in global scope representing Not-A-Number and is used in a mathematical function or operation in JavaScript which cannot return a specific number.

7) Explain passed by value and passed by reference.

Pass by value means that a copy of the actual parameter’s value is made in memory, i.e. the caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is not visible to the caller.

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller’s variable.

8) What is an Immediately Invoked Function in javascript?

Immediately-Invoked Function Expressions (IIFE), are a common JavaScript pattern that executes a function instantly after it's defined.

9. Explain Higher Order Functions in JavaScript.

Higher order functions are functions which take other function as a parameter or return a function as a value. The function passed as a parameter is called callback. Higher order functions return function as a value.

10. Explain “this” keyword.

The keyword “this” is set to the object when a method (when a function is a member of an object) of the object is called. The 'this' keyword refers to a special property of an execution context.

11. Explain call(), apply() and, bind() methods.

The call() method allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.The call() method takes arguments separately.

The apply() method allows you to call a function with a given this value and arguments provided as an array(The apply() method takes arguments as an array).
Here is the syntax of the apply() method:

fn.apply(thisArg, [args]);
Enter fullscreen mode Exit fullscreen mode

The bind() method returns a new function, when invoked, has its this sets to a specific value.The following illustrates the syntax of the bind() method:

fn.bind(thisArg[, arg1[, arg2[, ...]]])
Enter fullscreen mode Exit fullscreen mode

12. What is Currying in JavaScript?

Currying is a functional programming technique which transforms a function that takes multiple arguments into a sequence of nesting functions that each take only a single argument.

13. Explain Scope and Scope Chain in JavaScript.

Scope refers to the accessibility or visibility of variables in JavaScript. Scope determines which sections of the program can access the variable and where the variable can be seen.
Three are three types of scope in JavaScript:
a. Global scope
b. Local scope and
c. Block Scope

Scope Chain

The scope chain is how Javascript looks for variables. When looking for variables through the nested scope, the inner scope first looks at its own scope. If the variable is not assigned locally, which is inside the inner function or block scope, then JavaScript will look at the outer scope of said function or block to find the variable. If Javascript could not find the variable in any of the outer scopes on the chain, it will throw a reference error.

14. Explain Closures in JavaScript.

Closures are functions that have access to the variables that are present in their scope chain even if the outer function ceases to exist. Functions can be defined inside of other functions. The inner function has access to the vars and parameters of the outer function. If a reference to an inner function survives (for example, as a callback function), the outer function's vars also survive.

15. What are object prototypes?

Prototypes are a powerful and very flexible feature of JavaScript, making it possible to reuse code and combine objects. Prototypes are the mechanism by which JavaScript objects inherit features from one another.

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

16. What are callbacks?

A callback is a function which can be passed as parameter to other function. Callback is executed at the end of an operation, once all other operations are complete.

17. What is Memoization?

Memoization is a simple but powerful trick that can help speed up our code, especially when dealing with repetitive and heavy computing functions.

18. What is recursion in a programming language?

Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself.

19. What is the use of a constructor function in JavaScript?

a. A constructor function is used to initialize (set values for) the object's members/properties.
b. A constructor is also used to create a new object.

20. What is DOM?

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.

Top comments (0)