Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript block-scoped Variable9

// program showing block-scoped concept
// global variable
let a = 'Hello';

function greet() {

    // local variable
    let b = 'World';

    console.log(a + ' ' + b);

    if (b == 'World') {

        // block-scoped variable
        let c = 'hello';

        console.log(a + ' ' + b + ' ' + c);
    }

    // variable c cannot be accessed here
    console.log(a + ' ' + b + ' ' + c);
}

greet();
Comment

js make a variable block scoped

//The let and const keywords provide block-scoping
//Global scoped
let global = "I'm Global Scoped";

{
  //Block Scoped
  let a = "I'm Block Scoped";
  const b = "I'm Block Scoped";
  //Var is not block scoped(don't use var)
  var c = "I'm not Block Scoped";
}
//Variable a and b cannot be accessed as they are block scoped
console.log(a,b,c, global); //c and global can be accessed.
Comment

PREVIOUS NEXT
Code Example
Javascript :: replace characters form array 
Javascript :: Get specific elements from an object by using filter method 
Javascript :: reset value object js 
Javascript :: npm simple zip file creator 
Javascript :: Setting axios base url dynamically 
Javascript :: convert string to array javascript 
Javascript :: fade in onscroll jquery 
Javascript :: if or react 
Javascript :: multi-dimensional array js 
Javascript :: jquery autocomplete bootstrap modal 
Javascript :: ? operator in js 
Javascript :: how to subtract time in javascript 
Javascript :: push in object javascript 
Javascript :: ng select2 angular dropdown 
Javascript :: array remove last item 
Javascript :: Turn on modern JS by adding use strict to your script 
Javascript :: axios error handling 
Javascript :: js push object in array 
Javascript :: animate change of class angular 
Javascript :: Promise.all() with async and await 
Javascript :: button disable in js 
Javascript :: clone aJavaScript object 
Javascript :: circle progress bar react 
Javascript :: splice js 
Javascript :: javascript count number of clicks limit 
Javascript :: js naming conventions 
Javascript :: vue style 
Javascript :: js string to arraybuffer 
Javascript :: how to use data sets javascrip[t 
Javascript :: Modal dismiss react native by click outside 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =