10 JavaScript interview questions that you need to know.

1. Truthy and Falsy Value

//truthy value for a number
// '0', ' ', [], {} is a truthy value
const value = 4;
if(value){
console.log('true')
}
else{
console.log('false')
}
// output: true
//falsy value for a number
const value2 = 0;
if(value2){
console.log('true')
}
else{
console.log('false')
}
//output: false
// 0, '', undefined, null, NaN, false is a falsy value

2. Undefined vs NULL

3. Double equal vs triple equal

4. Scope, block scope

//example using let or const
function add(num1, num2){
let result = num1 + num2;
return result
}
const res = add(10,20)
console.log(res)
console.log(result) // can't access result outside of a function
//example using var
function add2(num1, num2){
var result2 = num1 + num2;
return result2
}
const res2 = add2(10,20)
console.log(res2)
console.log(result2) // now i can access result2 from here

5. Closure

function Watch(){
let count = 0;
return function(){
count++;
return count;
}
}
const clock1 = Watch()
console.log(clock1()) //1
console.log(clock1()) //2
console.log(clock1()) //3
const clock2 = Watch()
console.log(clock2()) //1
console.log(clock2()) //2
console.log(clock1()) //4

6. ‘This’ Keyword

const Student = {
firstName: 'Iqbal',
lastName: 'Ahmed',
fullName: function(){
return this.firstName + ' ' + this.lastName;
}
};

7. Window, Global Variable

var name = 'iqbal ahmed'
//now you can access this using window in your browser
conosole.log(window.name)
//output: iqbal ahmed
var name = 'iqbal ahmed';
function (){
var id = 173462142
console.log(id) /its a local variable, only can access here
console.log(name) //name is global i can access anywhere
}
console.log(id)// i can access it from here

8. Find the largest element of an array

var num = [30,0,20,40,50,100,90,60]
var max = num[0]
for(let i=0; i<num.lenght; i++){
let element = num[i];
if(element > max){
max = element;
}
}
console.log(max); // output:100

9. Remove Duplicate eLement from an array

var num = [10,20,10,43,20,54,76,43]
var uniqueNum = []
for(var i=0; i < num.length; i++){
var element = num[i];
var index = uniqueNum.indexOf(element);
if(index == -1){
uniqueNum.push(element)
}
}
console.log(uniqueNum) //output: 10, 20, 43, 54, 76

10. Count the number of words in a string

var mySpeach = 'Hi everyOne! This is a JavaScript Tutorial';
var count = 0;
for(var i=0; i< mySpeach.length; i++) {
var char = mySpeach[i];
if(char == ' ' && mySpeach[i-1] !== ' '){
count++;
}
}
count++;
console.log(count) //output: 7

--

--

--

Jr. Web Developer || Curious JavaScript Programmer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

React Native + Redux (Part 2)

Displaying an RSS Feed in CraftCMS

Simple svelte demo deployed in Netlify

ReactNative: how to pass callback param with react-navigation

Create MongoDB database models in Node.js

Extending the Mendix platform, the power of JavaScript Actions!

Top 7+ UI-Component Library for ReactJS 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Md. Iqbal Ahmed

Md. Iqbal Ahmed

Jr. Web Developer || Curious JavaScript Programmer

More from Medium

Understanding the Fusion Reaction through Magnets

Engineer to Engineer: Art is in the Code

My Journey into Web Development

What is Application Maintenance?