Get latest Web developer jobs weekly!

JavaScript interview Q&A

|

JavaScript interview questions & answers

1. What is JavaScript?

JavaScript is a scripting language used mostly in web programming. It is based on an object-oriented model and available in all modern browsers so it's a cross-platform language too. Mostly JavaScript is realized as part of HTML, where it can change itself and other elements of HTML scripts.

2. What are the differences between JavaScript and Java?

JavaScript doesn't strongly type one and requires additional integration in HTML to be executed, while Java supports multi-threading, and requires JDK installation to make it usable.

3. What data types are there in JavaScript?

There are two groups of data types in JavaScript: Primitive and Non-Primitive. The first group includes String, Boolean, Number, BigInt, Null, undefined and Symbol. The non-primitive data type is the Object.

4. What is the global variable?

It is the variable with global scope, so it is available from any part of the script.

5. What problems are associated with global variables?

Global variables stay in memory all time, have lower protection as inside the functions and may have conflicts with local variables with the same name.

6. What is the "null" in JavaScript?

The JS script returns "null" if the requested value is "empty". It may happen when you try to request a deleted element of the script or when a variable value was assigned with null to check it for changes during the script execution.

let a = null;
console.log(a);
//Output:
null

7. What is the "undefined" in JavaScript?

The "undefined" may be returned when you request a declared variable without any assigned value.

let a;
console.log(a);
//Output:
undefined

8. What is the "NaN" in JavaScript?

It's a bool property which shows that object is "Not-a-Number" or vice versa.

console.log(isNaN('Character'));
//Output:
true
console.log(isNaN('10'));
//Output:
false

9. What is the Array in JavaScript?

It is the kind of Object type which contains values placed in specified by user order and has special functions and properties to work with contained data.

10. What are the objects in JavaScript?

It is the basic entity of JavaScript with its own properties and their values. How to declare object:

let obj = { x: 10, y: 20, z: 30 };

11. What is the scope in JavaScript?

The scope is the area of the accessibility, and visibility of variables. The main principle of this concept is that if a variable is not in the scope, it cannot be referenced by functions and variables outside this scope.

let variableWithGlobalScope;
function someFunc(){
let variableWithLocalScope;
}

12. What is the callback?

It's a function which passed as an argument in another function, so they must be executed one-by-one.

13. What is an arrow function?

It is a short variant of declaring functions, using the "=>" symbol.

const numbers = [63, 15, 92, 20, 44];
console.log(numbers.filter((number) => number === 20));
//Output:
20

14. What is the difference between "=" "==" and "===" operators?

Their difference is in the used comparison algorithm.  The "===" compares values and data types, so it is more strict, while the "==" does the type conversion of operands before comparison. The "=" is the assignment operator used to assign value to variables.

15. What are the properties in JavaScript?

It is the basic characteristic of an object, generated by default and based on the structure of property name plus property value. For example all strings in JavaScript always have the property "length", which describes how many characters in it.

let testString = "Random text";
console.log(testString.length); //length is the property of any string
//Output
11

16. What are the attributes in JavaScript?

Attributes is the part of DOM, which describes additional attributes of the element as key-value pairs.

let someAttribute = document.getElementById("My Login").attributes.length;

17. How to write functions in JavaScript?

Depending on function difficulty it can be declared in three ways: as callback function, arrow function and inline callback function.

Callback function:

function testFunc1(){}
console.log(typeof(testFunc1));
//Output
'function'

Arrow function:

console.log(typeof(a => a + 10));
//Output
'function'

Inline callback function:

let a;
console.log(a);
//Output:
undefined
0

18. What is the difference between BOM and DOM?

Document Oriented Model is a versatile standard of interacting with web documents (like HTML) but inside in the browsers. Browser Oriented model is non-standardized and stands for interaction with browsers, for example its windows.

19. What is the "debugger" keyword in JS?

The debugger is the statement which makes possible using the debug functions, like a breakpoint.

20. What JS frameworks are in use?

The most known are: React (used for UI), Angular (web dev), Vue (UI), JQuery (client-side web dev), Ember (scalable web apps) and Node (server-side web dev).

21. What is enumerable in JavaScript?

It's the property of an object which with "true" state means that it is available for use in loops as it is enumerable.

22. What are the main JS advantages?

Versatility (cross-platform), easy for novices, weakly typed, client-side execution, prevails in web development.

23. Are there any JS disadvantages?

One-thread execution, strong potential of security vulnerabilities, single inheritance, may have different interpretations according to the selected browser.

24. What is an anonymous function in JS?

It is the function declared without name.

let a;
console.log(a);
//Output:
undefined

25. How to use JavaScript in HTML?

You can write JS code inside the <script>  tag, or initialize JS script from source, via script attribute.

let a;
console.log(a);
//Output:
undefined

26. What is the window.object?

It is part of the Browser Oriented Model which refers to a current window in the browser.

let a;
console.log(a);
//Output:
undefined

 27. How to use comments in JS?

There are two types of comments in JavaScript: one-line ("//" before it) and multi-line ("/*" before and "*/" after text).

let a;
console.log(a);
//Output:
undefined

28. What 2 groups of data types exist in JS?

Non-primitive which include only Object, and Primitive for 7 other data types.

Primitive and Non-Primitive. The first one includes String, Boolean, Number, BigInt, Null, undefined and Symbol. The non-primitive data type is the Object.

29. How to create objects in JavaScript?

To create an object in JS you need: write directive (let, const or var), then write variable name and close the string (";").To assign a value you need to write equal ("=") and the required value after the variable name.

let a;
console.log(a);
//Output:
undefined

30. How to create an Array in JavaScript?

Declare array name and assign its elements after the equal sign in square brackets. If the array doesn't have elements - write only brackets.

let a;
console.log(a);
//Output:
undefined

31. What is the Infinity in JavaScript?

Infinity is the Number object which represents the mathematical infinity, so it will always be bigger than any finite number.

32. Can Infinity be negative in JavaScript?

In JavaScript mathematical negative infinity is denoted as "-Infinity" . It is not equal to "Infinity" and always will be less than any finite number.

33. How to catch exceptions in script execution?

Put code inside try...catch construction.

let a;
console.log(a);
//Output:
undefined

34. What is the "this" keyword in JavaScript?

Depending on how it was invoked, the keyword "this" refers to an object, global object or element.

35. What is the strict mode in JavaScript?

This mode enables special rules of writing JS scripts, like mandatory variables declaring, extended reserved words list, prohibit numbers in octal system, using  "eval" statement for creating new variables and other.

36. What is the Boolean object in JavaScript?

Object which can have only two values: "false" or "true".

37. What is the Number object in JavaScript?

Object to store integer or floating point numeric values.

38. What is the Set object in JavaScript?

It is the collection of values, object references or primitives. It's main feature - it stores only unique elements.

39. What are the falsy values in JavaScript?

It is valued in JavaScript which evaluates to negative value. Here they are: false, undefined, null, NaN, 0, -0, 0n and empty string in any form ('' and "" and ``).

40. What is hoisting in JavaScript?

If variables and functions were declared not on the top of their scope, the interpreter during the compilation will place them at the top of the script to make it more stable before execution.

41. Is JavaScript based on an object-oriented model?

Yes, it is one of the key characteristics of JavaScript. But at the same time, some developers additionally classify it as a functional oriented language too.

42. How many ways to declare variables are there in JavaScript?

Variables in JavaScript can be declared with 3 directives: "var", "let" and "const".

43. What are possible Error name values in JavaScript?

There are 6 types of errors in JavaScript, which appear according to the nature of the error. Reference Error appear if use undeclared variable, Eval Error - error in eval , Range Error - when number outside the specified range, Syntax Error - mean incorrect syntax , while Type Error - tells that value is not in used data type and URI Error - is for using illegal characters.

44. What are the cookies in JavaScript?

Cookies it's a kind of data saved on a user's PC by some site via browser API. It is used for storing different sorts of data, from preferred site's color theme to signed-in accounts.

45. What loops are available in JavaScript?

The "for" loop with "for..in" and "for...of" variations, the "while" and "do...while" loops.

46. What is the polyfill in JavaScript?

It is the code which realizes modern functions unavailable in obsolete browser versions.

47. What are the ECMAScript standards?

It is the JavaScript standard, which include guidelines and describing of using JavaScript functions. ECMAScript compliance helps to unify JS scripts developing.

48. What is the curry function in JavaScript?

Currying divides the initial function that takes multiple arguments into several functions that each take only one.

49. What is the WeakMap in JavaScript?

It is an Object structure, collected from key-value pairs, where keys are objects.

50. Is JavaScript case-sensitive language?

Yes, it appends to both: reserved words and variables (or functions) names.