JavaScript es6
hosting
javaScript hosting is a default usage and this default usage is called javaScript hosting.
x=20
console.log(x)
var x
output-20
var declarations
Let Declarations
The let declaration syntax is the same as the syntax for var. You can basically replace var with let to declare a variable, but limit the variable's scope to only the current code block (there are a few other subtle differences discussed a bit later, as well). Since let declarations are not hoisted to the top of the enclosing block, you may want to always place let declarations first in the block, so that they are available to the entire block. Here's an example:
function getValue(condition) {
if (condition) {
let value = "blue";
return value;
} else {
return null;
}
}
Block Bindings Loop
In other languages, where block level scoping is the default, this example should work as intended, and only the for
loop should have access to the i
variable. In JavaScript, however, the variable i
is still accessible after the loop is completed because the var
declaration gets hoisted. Using let
instead, as in the following code, should give the intended behavior:
for (let i=0; i < 10; i++) {
process(items[i]);
}
// i is not accessible here - throws an error
console.log(i);
In this example, the variable i
only exists within the for
loop. Once the loop is complete, the variable is destroyed and is no longer accessible elsewhere.
Global Block Bindings Emerging Best
Another way in which let
and const
are different from var
is in their global scope behavior. When var
is used in the global scope, it creates a new global variable, which is a property on the global object (window
in browsers). That means you can accidentally overwrite an existing global using var
, such as:
// in a browser
var RegExp = "Hello!";
console.log(window.RegExp); // "Hello!"
var ncz = "Hi!";
console.log(window.ncz); // "Hi!"
Even though the RegExp
global is defined on window
, it is not safe from being overwritten by a var
declaration. This example declares a new global variable RegExp
that overwrites the original. Similarly, ncz
is defined as a global variable and immediately defined as a property on window
. This is the way JavaScript has always worked.
If you instead use let
or const
in the global scope, a new binding is created in the global scope but no property is added to the global object. That also means you cannot overwrite a global variable using let
or const
, you can only shadow it. Here's an example:
// in a browser
let RegExp = "Hello!";
console.log(RegExp); // "Hello!"
console.log(window.RegExp === RegExp); // false
const ncz = "Hi!";
console.log(ncz); // "Hi!"
console.log("ncz" in window); // false
Here, a new let
declaration for RegExp
creates a binding that shadows the global RegExp
. That means window.RegExp
and RegExp
are not the same, so there is no disruption to the global scope. Also, the const
declaration for ncz
creates a binding but does not create a property on the global object. This capability makes let
and const
a lot safer to use in the global scope when you don't want to create properties on the global object.
I> You may still want to use var
in the global scope if you have a code that should be available from the global object. This is most common in a browser when you want to access code across frames or windows.
Functions Related
Functions with default parameter values
JavaScript ES6 If you do not give a default value then it will show Undefited and if some value is blank then if you give any default value then it will show that value. Such as:
function myFunction (x = 10) {
return x;
}
console.log(myFunction())
output: defult vale = 10
unnamed parameters
When we create a function, if we don’t send a parameter to that function, it won’t work if we don’t call from outside. Again, if I call from outside, do it like:
function myFunction (){
console.log('My name is Liotn')
}
myFunction()output1-output2 -My name is Liton
spread operator
The spread operator is you. With … you will get all the values from where you want to get the value. For example:
function addNumber(x, y, z ){
return x+y+z;}
let number [1,3,6]console.log(addNumber(...number))