Answers for "function scope javascript"

2

function scope and block scope in javascript

/*It's all about the type of following keywords 
depending upon specific condition.
'var' is function scope.
'let' and 'const' are block scope.
Function scope is within the function.
Block scope is within curly brackets.For example:*/
var age = 50;
var temp=age+10;
if (age > 40){	
    var age=temp;
    console.log(`Your brother ${age} years old than you!`);
 }

//Output: Your brother 60 years old than you!

/*Note:But if we type console.log(age) outside the block scope than
the previous value of 'age' with 'var' keyword has been vanished that is:

>console.log(age);
>60

But in case of 'let' and 'const' keywords the previous value of
'var' in the previous example is not vanished here's an example:*/

var age = 50;
var temp=age+10;
if (age > 40){	
    let age=temp;
    console.log(`Your brother ${age} years old than you!`);
 }

/*Output: Your brother 60 years old than you!
Note: The output would still same but if we type 'console.log(age)' 
outside the block scope than the previous value of 'age' with 'var' keyword
has not been vanished that is:

>console.log(age);
>50
Posted by: Guest on August-03-2021
30

javascript define global variable

window.myGlobalVariable = "I am totally a global Var"; //define a global variable
var myOtherGlobalVariable="I too am global as long as I'm outside a function";
Posted by: Guest on August-02-2019
4

closure in javascript

function makeFunc() {
  var name = 'Mozilla';
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();
Posted by: Guest on July-04-2020
1

global scope js

const color = 'blue'

const returnSkyColor = () => {
  return color; // blue 
};

console.log(returnSkyColor()); // blue
Posted by: Guest on July-05-2020
4

closure in javascript

var counter = (function() {
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val;
  }
  return {
    increment: function() {
      changeBy(1);
    },
    decrement: function() {
      changeBy(-1);
    },
    value: function() {
      return privateCounter;
    }
  };
})();

console.log(counter.value()); // logs 0
counter.increment();
counter.increment();
console.log(counter.value()); // logs 2
counter.decrement();
console.log(counter.value()); // logs 1
Posted by: Guest on January-14-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language