24 quick-fire JavaScript interview questions
JavaScript Quickfire interview questions
Explain the difference between “==” and “===”
“==” is used to compare two values irrespective of the datatype of variable.
“===” is used to compare two values but will be a strict check so will check the value and the datatype match.
Example(s):
"50" == 50 // true
"50" === 50 // false
50 === 50 // trueHow do you check if a value is a number?
isNaN() function determines if the value is not a number.
If you useisNaN() you will have to do additional checks as you can see from the below examples.
Example(s):
isNaN(48) //false
isNaN(-1.23) //false
isNaN(5-2) //false
isNaN('123') //false
isNaN('Hello Im a real string') //true
isNaN('2005/12/12') //true
isNaN('') //false
isNaN(undefined) //trueHow do you convert a string to an int?
parseInt() converts a string to a whole number
Example;
parseInt("30", 10) // 30
parseInt("55px", 10) // 50
parseInt(2.55, 10) // 2parseFloat() converts a string to a point number (with a decimal)
Example:
parseFloat("30") // 30
parseFloat("55px") // 50
parseFloat(2.55) // 2.55You could also accept
Number() converts a string to a number. This can be a whole number or point number. These can often be less safe than using parseInt or parseFloat
Name the different loops in JavaScript
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
What is the difference between var , let and const?
var can be redeclared and updated.
let can be updated but not declared
const cannot be updated or redeclared.
Example:
var myNumber = 10;
var myNumber = 20;// No errorlet myNumber = 10;
let myNumber = 20;// In the console I get an error:
Uncaught SyntaxError: Identifier 'myNumber' has already been declaredWhat is the difference between NULL and undefined ?
NULL is an assignment value. It can be assigned to a variable as a representation of no value.
undefined means a variable has been declared but has not yet been assigned a value
Example:
let hi; // undefinedlet hi = NULL; //NULLWhat is the typeof operator used for?
typeof the operator returns a string indicating the type of the unevaluated operand.
Example:
typeof(122) // "number"
typeof(122.55) // "number"
typeof("I'm a string") // "string"
typeof({ name: "James Smith"}) // "object"
typeof(false) // "boolean"How do you check if an object is an array?
isArray() function determines whether an object is an array.
What is a callback?
A callback function, also known as a higher-order function, is a function that is passed to another function as a parameter.
Explain what default function parameters are
They allow named parameters to be initialized with default values if no value or undefined is passed.
Example:
function addTogether(x, y = 1) {
return x + y;
}addTogether(10, 10) // 20
addTogether(10) // 11What are ES6 modules?
They Organise a related set of JavaScript code. A module can contain variables and functions. A module is nothing more than a chunk of JavaScript code written in a file.
Name the two different ES6 exports
Default exports are used when a module only needs to export only a single value.
Named exports are distinguished by their names. There can be serval named exports in a module.
What are Promises?
They are used to handle asynchronous operations. They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events.
What are the different states in promises?
A Promise has three states:
- fulfilled: Action related to the promise succeeded
- rejected: Action related to the promise failed
- pending: Promise is still pending i.e not fulfilled or rejected yet
What is async/await?
async before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.
await makes JavaScript wait until that promise settles and returns its result.
What is the difference between local storage & session storage?
local persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
session changes are only available per tab. Changes made are saved and available for the current page in that tab until it is closed. Once it is closed, the stored data is deleted.
Name the different DOM selectors
- getElementsByTagName()
- getElementsByClassName()
- getElementById()
- querySelector()
- querySelectorAll()
What is a closure?
We need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.
How do you add an item(s) to an array?
You can use the push() function to append an item to an array. It can also add many items at the same time.
Example:
let items = ["dan", "john"];items.push("liam"); // ["dan", "john", "liam"]
items.push("lee", "james"); // ["dan", "john", "liam", "lee", "james"]How do you remove a specific item from an array?
splice function uses the index to remove an item from an array.
filter uses a callback function.
Why is JavaScript called a loosely typed or a dynamic language?
JavaScript is called a loosely typed or a dynamic language because JavaScript variables are not directly associated with any value type and any variable can be assigned and re-assigned values of all types.
Example:
let someLet = 10; // someLet is a number
someLet = "Hello world"; // someLet is now a string
someLet = true; // someLet is now a boolWhat is “this”?
The this keyword refers to the current object the code is being written inside.
How do you make an API call in JavaScript?
Looking for one of the below:
- XMLHttpRequest
- fetch
- Axios
- jQuery
Name the API verb types which you would use in JavaScript
Looking for the below:
- POST
- GET
- PATCH
- DELETE
- PUT
Conclusion
I’ve asked and been asked a mixture of the above questions in interviews. I think for any JavaScript interview its good to have a mixture of questions. A combination of a coding challenge(s) with some quick-fire questions is a good way to see a candidates potential.
Hope you’ve enjoyed!
In Plain English
Enjoyed this article? If so, get more similar content by subscribing to our YouTube channel!
