1)What is Javascript and why is it important?
High level interpreted programming language used to create interactive and dynamic content on websites.
2)
3)
4)
5)
6)
7)
8)
9)
10) What are different data types in JS?
Seven primitive data types:
Number--> Can’t hold large no.s
String --> “ ” or ‘ ‘ Boolean --> true/false
Null --> empty object or object with no value
Undefined--> doesn’t hold value, variable declared but no value assigned
Symbol --> new datatype to store anonymous and unique value . eg: var symbol1 = Symbol('symbol'); BigInt --> for large numbers.
--> Non-primitive / Compound data types object
arrays
11)What is hoisting in js?
--> Hoisting is default behavior of js where all the variable and function declarations are moved on top.
Example 1:
a=3;
console.log(a); // outputs 3 even when variable is declared after it is initialized.
var a;
Example 2:
hoistedFunc(): // outputs "Hello world!" even when the function is declared after calling
function hoistedFunc(){
console.log("Hello world!");
}
Example 3: // Hoisting takes place in the local scope as well
function doSomething(){
x=33;
console.log(x);
var x;
}
doSomething(); //outputs 33
Note: Variable initializations are not hoisted but only declarations are hoisted.
var x;
console.log(x): // outputs undefined since the initialization of x is not hoisted
x=30;
Note: To avoid hoisting run js in "use strict" mode on top of code:
"use strict";
x=40; // gives an error as x is not declared.
var x;
12) Difference between var and let keywords.
var and let both are used to declare the variables in JS. But there are some differences.
var has global scope means the value can be accessed from anywhere in the code or function scope accessible anywhere in the function, let has local scope or block scopemeaning the variable can be accessed within the block of code where it is declared.
13) Is javascript statically or dynamically typed language?
--> Dynamically(loosely) typed language --> type of variable is checked during run-time rather than statically typed language where type of variable is checked during compile time.
--> Static typing eg: String name; name ="John"; cannot have name = 34; Variables have types, values have types, variable cannot change type.
--> dynamic typing eg: var name; name="John"; name = 34; variables have no types, values have types, variables change type dynamically.
14) Explain passed by value and passed by reference.
--> In Js, primitive data types are passed by value whereas non-primitive passed by reference.
Passed by value (primitive datatypes): eg:
var y=40; // "=" operator allocates some space in memory where 40 is stored and returns the location of allocated memory space, so y is pointing to location not 40.
var z=y; // assign operator takes the value of y(40) and allocates new memory space and returns address. so variable z is not pointing to location of y but to new location in memory.
if y=23; // this wont affect value of z as z is pointing to other location not the location of y.
console.log(Z); // outputs 40.
passed by reference (non-primitive datatypes): eg:
var obj = { name:"Sabin", surname:"Khadka"};
var obj2 = obj; // "=" directly passes the location of variable obj to the variable obj2, pointing to same location/address in the memory.
// any change in obj creates change obj2.
15) Explain scope and scope chain in JS.
--> Scope helps us to know what functions and variables we can or cannot access at various part of code.
--> 3 types of scope: Global, Local/Function , Block scope.
--> Global Scope: