27.4 C
Gujarat
HomeJavascriptModern JavaScript

Modern JavaScript

Modern JavaScript

 

1) LET VS CONST  VS  VAR

variables declared with “var” in javascript are a function scoped.
variables declared with “let & const ” in javascript are a block-scoped.

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

Example1:

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

Example3:
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)

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
To enable you to solve more complex problems, ES6 template literals provide the syntax that allows you to work with strings in a safer and cleaner way.
In ES6, you create a template literal by wrapping your text in backticks as follows:

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> 
RELATED ARTICLES

7 COMMENTS

  1. 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.

  2. 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.

  3. 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!

  4. 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15,000FansLike
5,000FollowersFollow
535FollowersFollow
- Advertisment -spot_img

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Most Popular