Scope in JavaScript
Scope in JavaScript can call current context of execution .The context in which values and expressions are visible . We can imagine scope as a world .Every Scope as like a separate world .
JavaScript has a global scope in browser we can call it window scope
Let’s see a example :
var a=”20";
function(){
var b=”30"
}
console.log(a) //”20"
Here “a” is variable which in belongs to global scope
we can test with that
console.log(window.a) //”20"
##Parent Child Concept
Child can access variables of parent but parent can’t
and parent can change the value ass well
Let’s see:
var a=”20";
function(){
var a=”30"
}
console.log(a) //”30"
Execution Context
Execution context has two phase in JavaScript
- Creation-al phase
- Execution-al phase
Creation-al phase find out the declaration of variable and function store those things . For function this store reference id and for variable , definition of variable and set this value “undefined”
At Execution-al phase execute the function if this function is declare and for variable set actual value which is assign to variable
Var,Let And Const
Var is a function scope and let is a block scope
let’s see a example
function (){
for(var i=0;i<10;i++){
//something
}
for(let j=0;j<10;j++){
//something
}
console.log(i) //10
console.log(j) //undefined
}
#As let const is same but in const we cant refined his value
Primitive and reference data type
In short primitive type this data type is immutable
but reference type is mutable
— let’s see a example
let x=”ok”
const k={
g:”kalo”
}
x.toUpperCase()
console.log(x) // ok immutable
k.g=”alu”
console.log(k.g) //alu “mutable”
Try catch in JavaScript
We can handle error through try catch method in JavaScript
— -Syntax
try{
//try something to do
// if success then ignore catch error
}
catch{
// if fail to do then send error to user
}
Arrow Function
Arrow function introduced from ES6 is alternative for traditional function
— let’s see syntax
// Traditional Function
function aku(a){
return a + 100;
}
// Arrow function
let aku=a=>a+100
Default value Function
Default value function allow named parameter to be initialize default value if no value is assign or undefined is pass
— let see example
function multiply(a, b = 1) {
return a * b;
}
function sum(a, b = 1) {
return a + b;
}console.log(sum(5, 2));
// expected output: 7console.log(sum(5));
// expected output: 6
Rest operator
Rest operator allow function o accept an indefinite number of arguments as an array.
— Let’s See example
function allNum(a, b, ...manyMoreArgs) {
console.log("a", a)
console.log("b", b)
console.log("manyMoreArgs", manyMoreArgs)
}
allNum("one", "two", "three", "four", "five", "six")
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
Spread Syntax
Spread syntax can be used when all element of object or array need to include. We can use this for object cloning also
— let’s se example
const obj={
a:"a",
b:"b",
c:"c"
}let objClone = { ...obj }; // pass all key:value pairs from an object
Block
A block statement use to group zero or more statements
—
{
allstatements
}