Javascript Interview Question

Common Javascript interview question with answer

1. Tall me About JavaScript?

2. key features of javascript?

  • lightweight
  • Interpreted Programming language.
  • Open source Cross-Platform

3. Disadvantages of javascript?

  • No support for multithreading.
  • No support multi-process.
  • It can’t be used for networking applications.

4. What is an argument object?

function func1(a, b, c) {
console.log(arguments[0]);
// expected output: 1
console.log(arguments[1]);
// expected output: 2
console.log(arguments[2]);
// expected output: 3
}
func1(1, 2, 3);

ref:mdn

5. How anonymous function assigned to a variable?

const display = () => {
console.log("anonymous function");
};

6. What is closure in Javascript?

7. Is javascript case sensitive language?

8. What Is the main difference between window and document?

9. Does Javascript synchronize?

10. Does Javascript allow multiple spaces?

11. What are the differences between null and undefined?

12. What are the differences between == and ===?

13. How would you compare two objects in JavaScript?

JSON.stringify(obj1) === JSON.stringify(obj2)

14. How do I redirect to another webpage?

15. How do you get query string values?

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');

16.How to call a function when the DOM is ready for it.

$('document').ready(function(){});

in standard JavaScript with no library

window.attachEvent("onload", fn);

17. How to convert a JSON string into an object?

18. How to Find the child element of the parent?

<div class="parent">
<div class="child1">
<div class="child2">
</div>
</div>
</div>
let parent = document.querySelector('.parent');
let children = parent.children; // [<div class="child1">]

19. How do you Get the current origin?

https://www.w3schools.com/js/js_ajax_intro.asp
window.location.origin // https://www.w3schools.com

20. How do you get the object’s keys and return them into an array?

const obj1 = {
name: "Jobayer Hossain",
id: 21,
};
const keys = Object.keys(obj1);
// ["name", "id"];

21. How do you toggle a class name?

const el = document.querySelector('.title')
el.classList.toggle('title-toggle');
// true and false

22. What is Closure?

23. How to empty an array in JavaScript?

var arrayList =  ['a', 'b', 'c', 'd', 'e', 'f'];//Method 1
arrayList = [];
//Method 2
arrayList.length = 0;
//Method 3
while(arrayList.length) {
arrayList.pop();
}

24. What is negative infinity?

25. Which company developed JavaScript?

26. Cookie

Answer: Set the expires parameter to a passed date

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

27. Local storage & Session storage

Answer: Local Storage

  • Don't send the request to the server.
  • it’ will stay until manually cleared.

Session Storage

  • Storage gets cleared when the page session ends.
  • Storage has an expiration time

28. innerHTML & innerText

Answer:

innerHTML — It will process an HTML tag that is found in a string.

innerText — It will not process an HTML tag is found in a string

29. NaN

Answer: When a string or something else is being converted into a number and that cannot be done, then we get to see NaN. it always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number

30. DOM

Answer: It is a tree-like structure that is modeled from the HTML document. The DOM is applied for combining and transforming the DOM structure or particular Elements or Nodes.

31. Event

Answer: event.target is the element on which the event occurred or the element that triggered the event.

32.Hoisting

Answer: Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution

33. Use strict

Answer: That makes our code in Strict Mode in functions or entire scripts. Strict Mode helps us avoid bugs early on in our code and adds restrictions to it.

34. Backtick

Answer: Template Literal by using the backtick or back-quote symbol.

35. Attributes and properties

Answer:

Attributes- Attributes provide the information about the details given to objects like name, age, id, etc,

Properties- Properties gives information about the values given to the objects.

36. BOM and DOM

Answer:

  • BOM- BOM is a Browser Object Model, in which the window object is supported by all the browsers. The JavaScript objects, variables, and functions also become members of the window object. This model deals with the objects of browsers like location, navigator, history, and screen.
  • DOM- DOM is a Document Object Model in a JavaScript that helps to access document and HTML elements. When a web page is loaded the browser creates a DOM for the page. All the objects on the page all arranged in an order.

37. Delete keyword

Answer: Using, delete object.propertyName is used to remove the property from the JS object.

38. Continue statement

Answer: JavaScript continue statement is used to bypass the specified iterations of a loop so that the code in the loop statement does not execute for the specified iterations and moves on to the next

39.Errors

Answer: In JavaScript, there are the following three types of errors:

  1. Syntax Error
  2. Runtime Error
  3. Logic Error

40.Runtime Errors

Answer: Runtime errors inform the user, that there is a problem with a script of the error along with a line number is specified in an alert box, easily search for the script. Runtime errors mostly occur due to improper use of the commands.

Javascript Codeing Problem with Solution

Image by Arek Socha from Pixabay

41. Word Count

Answer: Frist we trim all space around this string then split with whitespace and finally we return a value of whitespace length.

const word = "waiting for changes before restart";function wordCount(text) {
const trim = text.trim();
return trim.split(" ").length;
}
wordCount(word);
//5

42. Sentences Count

Answer: In given string first split in word array and then map throw within array and condition with possible ending line sign.

function senCount(str) {
let count = 0;
str.split("").map((x) => {
x == "?" || x == "!" || x == "." ? count++ : count;
});
return count;
}
const string = "Lorem ?Ipsum ! is simply dummy text.";
senCount(string);
//3

43. Vowel Finding

Answer: The indexOf method returns the index within the value position when it does not exist then it returns -1 value. So first we convert a string into an array with the split method. then map and condition.

function vowelCount(str) {
const vowelList = "aeiouAEIOU";
let count = 0;
str.split("").map((x) => vowelList.indexOf(x) !== -1 && count++);
return count;
}
vowelCount("The quick brown fox");
//5

44. Remove Duplicate

Answer: There are multiple ways to remove duplicate. In this way, we loop with map function and conditionally push defending on position value.

const array = ["Mike", "Mike", "Nancy", "Nancy", "Carl"];function removeDuplicates(arr) {
const newArray = [];
arr.map((x) => {
newArray.indexOf(x) === -1 && newArray.push(x);
});
return newArray;
}
removeDuplicates(array)
//[ 'Mike', 'Nancy', 'Carl' ]

45.Palindrome

Answer: Frist I valid this word with converting lowercase and removing whitespace and then compare with reverse word within parameter word.

function isPalindrome(input) {
const toValid = input.trim("").toLowerCase();
const reverseWord = toValid.split("").reverse().join("");
return reverseWord == input.toLowerCase().trim() ? true : false;
}
isPalindrome(" madam ");
//true

46. Recursive number

function recurse(n) {
return n < 1 ? 1 : n * recurse(n - 1);
}
recurse(2);

47. Generate Even number

Answer:

function eNumber(n) {
let nums = [];
for (let i = 1; i <= n; i++) {
let ele = i * 2;
nums.push(ele);
}
return nums;
}
eNumber(5);
//[ 2, 4, 6, 8, 10 ]

48. Array Item Remove

Answer: Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

const array = [2, 5, 9];console.log(array);const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
// array = [2, 9]
console.log(array);

49. Object Property Remove

Answer: Useing delete keyword to remove specific property from an object.

var myObject = {
"name": "JO\obayer",
"id": "234",
};
delete myObject.id;

50. Array Value Check

Answer: The general pattern is

console.log(['joe', 'jane', 'mary'].includes('jane')); //true

Front End Developer