By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Modern JavaScript
JavaScript

Modern JavaScript

Rohan750
Last updated: 2021/05/16 at 5:37 AM
Rohan750
Share
4 Min Read

 

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.

Contents

 

1) LET VS CONST  VS  VARVarlet const2)template literals (template strings)3) Default Parameters4)Fat Arrow FunctionBasic Syntax

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

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Functions in JavaScript
Next Article Arrays in JavaScript
7 Comments 7 Comments
  • Essie says:
    November 16, 2021 at 7:11 pm

    This post provides clear idea in support of the new visitors of blogging, that truly how to do
    running a blog.

    Reply
  • Deanne says:
    November 18, 2021 at 9:13 pm

    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.

    Reply
  • Salvatore says:
    November 22, 2021 at 6:16 pm

    Nice answer back in return of this question with solid arguments and explaining
    everything about that.

    Reply
  • t.co says:
    March 23, 2022 at 3:31 pm

    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.

    Reply
    • Admin says:
      March 26, 2022 at 6:23 am

      I am thinking to changing the website theme

      Reply
  • http://bit.ly/3NeyDzh says:
    March 24, 2022 at 4:02 am

    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!

    Reply
  • tinyurl.com says:
    March 24, 2022 at 2:15 pm

    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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?