Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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.
 
PREVIOUS NEXT
Tagged: #js #variable #block #scoped
ADD COMMENT
Topic
Name
8+4 =