[2021] Top 10 Javascript beginners interview questions and Answers
1. What are JavaScript Data Types?
JavaScript has 5 Data types and these are,
- Number
- String
- Boolean
- Object
- Undefined
2. What is the difference between == and === in Javascript ?
== [ comparison operator ]
- This compares only the value
- ‘1’ == 1 returns true because string of value 1 and 1 as number both has the value as 1.
=== [ strict comparison operator ]
- This compares both value and type
- ‘1’ === 1 returns false because string of value 1 and 1 as number has the value as 1 but the type is string and number so returns false.
3. What is the difference between let, var and const
let:
- block scope
- value once assigned can be reassigned multiple time.
- let variable can be declared without initialization (eg: let l ), default value will be assigned as “undefined”
var:
- function scope
- value once assigned can be reassigned multiple time.
- var variable can also be declared without initialization (eg: var v), default value will be assigned as “undefined”
const:
- block scope
- value once assigned can not be reassigned again.
- const variable can not be declared without initialization (eg: const c; -> not possible, const c1 = “ngdev”; -> possible)
4. What are the different loops available in Javascript ?
- for
- for each
- while
- do-while
5. What is the difference between null and undefined ?
null:
- null value also means no valid value / empty value
- we can use wherever we want to assign empty / no valid value.
- typeof null returns “object”, so null is considered as the object in javascript.
undefined:
- undefined also means no valid value / empty value
- Javascript assigns the undefined value to the uninitialized values. (we can also assign the same wherever required, but not recommended).
- typeof undefined returns “undefined” only.
6. What is dynamically typed language or Is javascript dynamically typed language ?
When you are creating the variable, you don’t want to worry about the value type you are going to assign in javascript as this is the dynamically typed language, which means value types can be changed during run time.
Example:
var pincode;
pincode = '600000' // here 600000 is the string value
// again to the same variable pincode you can assign the number value as well
pincode = 600000 // here 600000 is the number value. // works without any issues
7. What is isNan in Javascript ?
Nan (Not-A-Number) to check the provided value is the number or not.
isNan() -> returns true if the provided value is not a number and returns false if the provided value is number.
console.log(isNaN("ngdeveloper.com")); // Returns true as "ngdeveloper.com" is not a number
console.log(isNaN(111)); // Returns false as 111 is the number
console.log(isNaN('111')); // Returns false, as '111' from string can be converted to Number type
console.log(isNaN(true)); // Returns false, since true converted to Number type (1)
console.log(isNaN(false)); // Returns false, since false converted to Number type (0)
console.log(isNaN(undefined)); // Returns true, as undefined is not a number
8. What is the difference between break and continue statement?
break:
- break statement breaks the loop
continue:
- continue statement skips/breaks the current iteration.
9. Write the sample snippet to write the array & object in javascript ?
Creating an object in javascript:
var company = {
name: "Ngdeveloper",
domain: "ngdeveloper.com"
};
Creating an array in javascript:
var inputArray = [1, 2, 3, 4, 5];
10. What is typeof in javascript ?
typeof is a javascript keyword that returns the value of the variable.
Few examples:
console.log(typeof(1) == "number"); // Returns true, as 1 is of type "number"
console.log(typeof("ngdeveloper.com") == "string"); // Returns true as "ngdeveloper.com" is of type "string"
console.log(typeof(null) == "object"); // Returns true as "null" is of type "object"
console.log(typeof(undefined) == "undefined"); // Returns true as undefined is of type "undefined"
console.log(typeof(true) == "boolean"); // Returns true as "true" is of type "boolean"
console.log(typeof([]) == "object"); // Returns true as "[]" is of type "object" not "array"