JavaScript Interview Questions

Q1. why do we call javaScript a dynamic language?
-> JavaScript is a dynamic language that means data types of the variable can change during the runtime.
EX:-
var x = 0 //number
x ++;
x = "text1"; // stirng
x = true // boolean
Q2. How does javascript determine data types?
-> JavaScript determines data types depending on the value assigned.
EX:-
var x = 100;
var whatisdatatype = typeof(x); // number
x = "SomeString";
whatisdatatype = typeof(x); // string
x = true;
whatisdatatype = typeof(x); //boolean
Q3. what is typeof functions?
Q4. How to check data type in JavaScript?
-> We can get the datatype by using the "typeof" Function.
Ex:-
var x = 100;
var whatisdatatype = typeof(x); // number
x = "SomeString";
whatisdatatype = typeof(x); // string
x = true;
whatisdatatype = typeof(x); //boolean
Q5. What are the different datatypes in JavaScript?
In JavaScript two types of datatypes " Primitive" and "Object"
// Primitive
var str = "shiv"; //string
var num = 10; //number
var nu = null; //null
var und = undefined; //undefined
var bool = true; //boolean
var big = 100 // big int
var Sym = Symbol(); // symbol
// Object
Var obj = new object(); //object
Q6. Explain Undefined data types?
-> Undefined means the variable has been declared but no value is assigned to it.
EX:-
var y = 10;
var z = "shiv";
var x ; // undefined
Q7. what is Null?
-> Null is a data type that indicates the intentional absence of data. Null indicates it's not zero, it's not empty it's just absence of data.
EX:-
var y = 10;
var z = "shiv";
var x;
var emptyString ="";
var zero = 0;
var n = null; //null
Null
We assign value null, it indicates absence of data.
Undefined
Variable has been created but value is not assigned.
Q9. Explain Hoisting.
-> JavaScript Hoisting is a/it's a mechanism where variables and function declaration are moved to the top of the scope.
EX:-
console.log(y); //y is undefined
var y = 10;
Q10. Is JavaScript initialization hoisting?
-> No
Q11. What are Global variables?
-> Global variables are accessible through out the webpage or the document.
EX:-
var x = 10;
function fun(){
console.log(x);
}
fun();
Q12. What are the issues with global variables?
-> Problem is global variable very hard to debug.
Q13. What happens when you declare a variable without var?
-> without a declared var variable is become global.
EX:-
var x = 10;
function fun1(){
y=100;
console.log(x)
}
fun1();
console.log(y);
Q14. What is use Strict?
Q15. How to Force developers to use var keyword?
-> "Use Strict" strictly checks if the variable is defined using "var" Keyword.
EX:-
"use strict";
var x = 10;
function fun1(){
y =100;
console.log(x);
}
fun1();
console.log(y);
Q16 How can we handle global variables?
Q17. How can we avoid global variabels?
-> It's difficult to avoid global variables. But we can organize it property by doing two things-
- Put global variable in a proper Namespace.
- Module pattern using closures and IIFE.
Q18. What are Closures?
Q19. Why do we need Closures?
-> Closures are fuctions inside function and it makes a normal function stateful.
EX:-
function SimpleFunction(){
var x = 0;
x ++;
}
function closureFunction(){
var x =0;
function Increment(){
x ++;
}
return{
increment
}
}
SimpleFunction();
SimpleFunction();
var ref = closureFunction();
ref.Increment();
ref.Increment();
Q19. Why do we need Closures?
gol of closure
Self-Contained Modules.
Self-Contained State.
Solves the global variable problem.
Q20. Explain IIFE? (its stand for Immediately Invoked Function Expression)
-> Its an anonymous function which gets immediately invoked.
EX:-
var x = 0 ;
( function () {
var y = 10;
alert(" I am IIFE and will execute one wher page loads");
})();
alert(y);
Q21. what is the use of IIFE?
Q22. what is name collision in global scpe?
Q23. IIFE VS Normal function?
function init(){
var x =0;
}
var init =0;
init();
Name collision happens when same name function name and variable names are declared.
Q.Why this is collision
-> Because IIFE does not have name, so there is no way can get name collision
Q. What is the different between normal function vs IIFE.
-> A normal function has a name while IIFE does't have name. So with a normal function you can have a name collision but with IIFE you will not have name you will not name collision.
Q24. What are design patterns?
-> Design patterns are time tested architecture solutions.
So, to create a Single instance we can use singleton pattern, if the object creational process is complex. we can use factory pattern and so.
Q25. which is the most used design pattern?
-> Module design pattern or the module revealing pattern.


