let and const are hoisted to the top of the block they
are in but not initialized to the default undefined value so
we can not use them until they are declared.
If we do,RerefenceError is thrown.
Variables declared using var can be accessed before they are declared and are equal to undefined.
Remember hoisting means declarations are moved to the top of their scope not the initialization.
Category: Javascript
Global execution context and Syntax parser
Global execution context : “When any code run in JavaScript its inside in Global execution context.Global execution context create Global object,this variable, Outer environment”
Syntax parser: A program that read your code and determine what is does and if its grammar is valid. Your code convert to computer Instruction
JSON.stringify() VS JSON.parse()
Literal object can be sent from client to server by JSON.stringify();
And Others side String json file from server we can convert to literal object by JSON.parse()
Function borrowing and currying
Function borrowing…..
var person = { firstName : "atik", lastName : 'haque' getName : function (){ return this.fristName + "" + this.lastName; } } var newPerson = { firstName : 'kamal', lastName : 'khan' } person.getName.call(newPerson);
Function currying…..
function MultipleBy(a,b) { return a * b; } var multipleByThree = MultipleBy.bind(this,3); multipleByThree(5);
In javascript Creation phase and Execution phase
In javascript Creation phase and Execution phase. First phase is Creation phase.
In that phase create memory space for every variable and function that you write in your code.
So when the execution phase will run it can find it in memory.
And when the execution phase run it compile line by line and execute the code.Execution phase run single threaded Synchronous manner that means always execute single line at a time in order.
What is the difference between == and === operator
Firstly
"=="
this equality check operator try to Coercion the two value
like
3 == '3'
in this case == operator try convert ‘3’ in number thats why
3 == '3' return true;
another example
1 < 2 < 3
what might you think the result will be ? result is true.
On the other hand
3 < 2 < 1
resutl also true. You may be confuse ……..
let see what is going here…
firstly example 1
"1 < 2 < 3"
this
<
comparison operator Associativity is left-to-right so this
'<'
comparison operator Associativity is left-to-right so
1 < 2
execute first and result true
then
true < 3;
here true is going to convert number and converted result is 1
then
1 < 3
result is true.
But on the other hand
'==='
Strict Equality operator dont compare in this fashoine.
It just compare the value without convert or Coercion.
Like
3 == '3' return true;
but
3 === '3' return false;
So main difference between
"=="
and
"==="
this
"=="
convert the value
and this
"==="
dont convert the value
Immutability in Javascript…..
The string type (primitive type) value is immutable that means you cannot change
its value without reassign. No string method can change its value its only create another one. In example
var stringLiteral = 'I am string literal and I am immutable'; //Firstly using method console.log(stringLiteral.slice(5,11)); //it will print string" console.log(stringLiteral); //"I am string literal and //I am immutable its value remain unchange //and secondly we can not add method in string literal stringLiteral.prop = "I am new method"; console.log(stringLiteral.prop); // ->; undefined
But the String object, which is created by using the new String() constructor, is mutable, because it is an object and you can add new properties to it
var stringObject = new String('I am string literal and I am mutable'); // on the others side // we can add a new property in string object stringObject.newProp = "Within string object i am a new property "; console.log(stringObject.newProp); // Within string object i am a new property var newStringObject = stringObject; stringObject.newProp = "I am mutable"; console.log(newStringObject.newProp); // I am mutable
Hoisted concept in javascript
Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting. Variables in JavaScript are in a sense “hoisted” or lifted to the top of the function or statement. However, variables that are hoisted will return a value of undefined. So even if you declare and initialize after you use or refer to this variable, it will still return undefined.
/** * Example 1 */ console.log(x === undefined); // true var x = 3; /** * Example 2 */ // will return a value of undefined var myvar = "my value"; (function() { console.log(myvar); // undefined var myvar = "local value"; })();
One Important things to remember is Only function declaration gets hoisted to the top and not the function expression.
/* Function declaration */ HoistedFun(); // "I am hoisted" function HoistedFun() { console.log("I am hoisted"); } /* Function expression */ FunctionExp(); // TypeError:FunctionExp is not a function var FunctionExp = function() { console.log("Hoisetd"); };
Block scope, Function Scope, Lexical Scoping in Javascript
Function Scope means that any variable which is defined within a function is visible within that entire function not out side of the function.
function funScope () { var iAmLocalScope = "I am Local"; console.log("Function local scope: " + iAmLocalScope); } funScope(); console.log(iAmLocalScope); // -> "ReferenceError: iAmLocalScope is not defined
Block Scope, a variable scope is limited by the block where it is declared in. A block is usually means between {} curly braces or loop variables for(;;){}.It is not possible to create a block scope in javascript. And here the example
var i = 1; // <--- In here first i is define and initialize to 1 for (var i = 0; i < 10; i++) { // <-- Here also define the same variable i with block for loop{} } console.log(i); // <-- 10 //if block scope exist then i value //should print here 1 instead of 10.
But This behavior changes, when using the let declaration introduced in ECMAScript 2015.
var i = 1; // <--- In here first i is define and initialize to 1 for (let i = 0; i < 10; i++) {} // <- using by let local scope create within block scope console.log(i); // <-- Thats why i value is 1 instead of 10
Lexical Scoping(Static Scoping or Closure) : If a child function define and call within a parent function scope then child function can access all the local variable within the parent function scope even if the parent function has returned
var adder = function (loc) { return function (summand) { loc += summand; console.log(loc); }; }; // <- we call the annonymous function // and assign the returned function to adder var inner = adder(1); // -> Now local variabe loc == 1 inner(5); //-> Now local variabe loc == 6 inner(4); //->Now local variabe loc == 1
Javascript Data types
According to ECMAScript standard Javascript has seven data types. Six data types that are primitives and one is non primitive
This are the primitive datatype
- Boolean. true and false.
- null. A special keyword denoting a null value. Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.
- undefined. A top-level property whose value is undefined.
- Number. 42 or 3.14159.
- String. “Howdy”
- Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.
And non primitive is Object
The
undefined
value behaves asfalse
when used in a Boolean context but in numeric context it behave like NaN
var myVal; console.log(typeof myVal);//"undefined" // In boolean context if(!myVal) console.log("I am undefined");//"I am undefined" // In numeric context console.log(myVal * 4);//NaN