What is an IIFE? or What is an IIFE and Why are they used?
- Immediately invoked function expression
- A function that is executed right after it is created.
- As you know that a function in JavaScript creates the local scope. So, you can define variables and function inside a function which cannot be access outside of that function. However, sometime you accidentally pollute the global variables or functions by unknowingly giving same name to variables & functions as global variable & function names. For example, there are multiple .js files in your application written by multiple developers over a period of time. Single JavaScript file includes many functions and so these multiple .js files will result in large number of functions. There is a good chance of having same name of function exists in different .js files written by multiple developer and if these files included in a single web page then it will pollute the global scope by having two or more function or variables with the same name. Consider following example of two different JavaScript file included in single page.
// this is IIFE function
(function dobleNumber2(num1) {
return num1 * 2;
})(20); //Output: 40
Immediately
What are different data types in JavaScript?
- Primitive Data Type
1. Number: Represent numeric values, both integer and float.
2. String: Sequence of characters are represent using string.
3. Boolean: Represent Boolean value, true or false.
4. Undefined: Represent Undefined Value.
5. Null: Represent Null. - Non-primitive Data Type
1. Object: Represent more complex data structure.
2. Array: Represent Group of elements.
3. RegExp: Represent Regular Expression.
Explain Variables in JavaScript?
- Basically, variable is used for temporary storage of data.
- It has name value and memory address.
- You have to declare variables before using it for storing data.
eg. var variableName;
What is var?
- The var statement declares a variable and can also optionally initialize its value.
- var is function scoped when it is declared within function. That means that it is available and can be accessed only within that function
- var are function scope because their visibility is limited to function. When you try to use outside the function you'll get an error
function myFun(){
var fname="Pradeep";
console.log(fname); //Output : Pradeep
}
console.log(fname); //Output : ReferenceError: fname is not defined
or
var x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 2
What is let?
- The let allows you to declare variables that are limited in scope to the particular block scope.
- block scope {} between the code
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 1
What is Const ?
- Const are block scoped, much like variables defined using the let statement. the value of const cannot change through reassignment, and it can't be re-declared.
const number = 42;
try {
number = 99;
} catch (err) {
console.log(err);
// expected output: TypeError: invalid assignment to const `number'
// Note - error messages will vary depending on browser
}
console.log(number);
// expected output: 42
What is difference between let and var?
- The variable defined with var is available anywhere within the function hence var keyword has function scope.
- The let has a block scope. a variable declared with let keyword has scope only with in that block.
What is difference between let and const?
- let allows you change the value of variable any number of times.
- Using const, after the first assignment of the value we cannot redefine the value again.
let name = 'Pradeep'
console.log(name); // Output: Pradeep
const handle = 'Something Happen';
console.log(handel) // Output: Something Happen
name = 'Neha'
console.log(name)// Output: Neha
handle = 'Nothing Happen'
console.log(handel) // Output: Assignment to constant variable.
What is the difference between comparing variables using '==' and '===' operator
- the '==' operator tests for abstract equality. Checks whether its two operands are equal
console.log(1 == 1);
// expected output: true
console.log('hello' == 'hello');
// expected output: true
console.log('1' == 1);
// expected output: true
console.log(0 == false);
// expected output: true
- the '===' The strict equality operator. checked whether its two operands are equal, returning a Boolean result. the strict equality operator always considers operands of different types to be different.
console.log(1 === 1);
// expected output: true
console.log('hello' === 'hello');
// expected output: true
console.log('1' === 1);
// expected output: false
console.log(0 === false);
// expected output: false
Hosting in JavaScript?
Variable Hosting: Hosting is JavaScript default behavior of moving declaration to the top of the function. if defined in a function, or the top of the global context, if outside a function.
var a; this is called variable declaration.
var a = 'test'; this is called variable initialization.
variable can be used before it has been declared.
//normal JavaScript code
a=10;
console.log(a);
var a;
// JavaScript compile phase
var a;
a =10;
console.log(a)
only variable declarations are hostied to the top, not variable initialization.
var a =10;
console.log(a+''+b);
var b = 20
output : 10 undefined
JavaScript compile phase
var a;
var b;
a=10;
console.log(a+''+b);
b=20;
Function variable hosting
var i = "Hello";
console.log(i);
function myFun(){
console.log(i);
var i = "Pradeep Hi";
}
myFun();
Output: Hello Undefined
JavaScript compile phase
i = "Hello"
var i;
console.log(i) // output Hello print
function myFun(){
var i;
console.log(i) // Undefined
i = "Pradeep Hi" }
When Using let and Const Hosting
let a = 'Pradeep';
console.log(a)
// Output: Pradeep
Hosted let same as const
console.log(a);
let a = 'Pradeep';
Output: Reference Error
What is difference between undefined and null?
Undefined means a variable has been declared but has no value assigned.
Null means the value has been set to be empty.
var a = null; this is Null
var a; this is undefined.
What is output of null == undefined?
- null == undefined will return true
- null === undefined will return false.
Comments
Post a Comment