1) LET VS CONST VS VAR
var variables can be updated and re-declared within their scope. let variables can be updated but not re-declared. const variables can neither be updated nor re-declared.
Var
var myName = "Rocoderes";
console.log(myName);
myName = "coderes";
console.log(myName);
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
Rocoderes
coderes
PS C:UsersThe-BeastDesktopjavascript Blog>
Example2:
function varDemo() {
var myFirstName = "Ro";
console.log(myFirstName);
if(true){
var myLastName = "Coderes";
}
console.log('innerOuter ' + myLastName);
}
varDemo();
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
Ro
innerOuter Coderes
PS C:UsersThe-BeastDesktopjavascript Blog>
let
let myName = "ro";
console.log(myName);
myName = "king";
console.log(myName);
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
ro
king
PS C:UsersThe-BeastDesktopjavascript Blog>
Example4:
function varDemo() {
let myFirstName = "Ro";
console.log(myFirstName);
if(true){
let myLastName = "Coderes";
}
console.log('innerOuter ' + myLastName);
}
varDemo();
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
Ro
C:UsersThe-BeastDesktopjavascript Blogindex.js:736
console.log('innerOuter ' + myLastName);
^
ReferenceError: myLastName is not defined
const
Example5:
const myName = "rocoderes";
console.log(myName);
myName = "coderes";
console.log(myName);
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
rocoderes
C:UsersThe-BeastDesktopjavascript Blogindex.js:723
myName = "coderes";
^
TypeError: Assignment to constant variable.
Example6:
function varDemo() {
const myFirstName = "Ro";
console.log(myFirstName);
if(true){
const myLastName = "Coderes";
}
console.log('innerOuter ' + myLastName);
}
varDemo();
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
Ro
C:UsersThe-BeastDesktopjavascript Blogindex.js:736
console.log('innerOuter ' + myLastName);
^
ReferenceError: myLastName is not defined
const variables can neither be updated nor re-declared.
2)template literals (template strings)
Example:
let demo = `This is a template literal`;
Example: JavaScript program to print table for a given number (5)
for(let num = 1; num<= 10; num++){
let tableOf = 5;
console.log( ` ${tableOf} * ${num} = ${tableOf * num}` );
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
PS C:UsersThe-BeastDesktopjavascript Blog>
3) Default Parameters
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Example:
function mult(a,b=2){
return a*b;
}
console.log(mult(5));
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
10
PS C:UsersThe-BeastDesktopjavascript Blog>
4)Fat Arrow Function
Arrow functions were introduced with ES6 as a new syntax for writing JavaScript functions. They save time and simplify function scope.
Basic Syntax
// Basic Syntax with One Parameter
const oneParameter = phrase => phrase.split(" ");
console.log(oneParameter("Arrow Function is Awesome"));
// Basic Syntax with Multiple Parameters
const multipleParameter = (x, y) => { return x * y };
console.log(multipleParameter(20,10));
// No Parameters
var noParameter = () => { console.log("hello world"); };
noParameter();
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
[ 'Arrow', 'Function', 'is', 'Awesome' ]
200
hello world
PS C:UsersThe-BeastDesktopjavascript Blog>
This post provides clear idea in support of the new visitors of blogging, that truly how to do
running a blog.
I’ve read several excellent stuff here. Certainly worth bookmarking for revisiting.
I surprise how so much attempt you put to create this sort of great informative site.
Nice answer back in return of this question with solid arguments and explaining
everything about that.
hey there and thank you for your information – I have definitely picked up something new from right here.
I did however expertise a few technical issues using this website, since
I experienced to reload the site lots of times previous to I could get it to load properly.
I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances
times will very frequently affect your placement in google and
can damage your high quality score if ads and
marketing with Adwords. Anyway I am adding this RSS to
my email and could look out for a lot more of your respective interesting content.
Ensure that you update this again very soon.
I am thinking to changing the website theme
Hi! I could have sworn I’ve been to this blog before but after checking through some of the post I realized it’s new to me.
Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back
frequently!
I blog frequently and I truly appreciate your information. Your article has really peaked my interest.
I will book mark your website and keep checking for new information about once a week.
I subscribed to your Feed as well.