Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2136

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T02:38:08+00:00 2024-11-26T02:38:08+00:00

Var, Let , Const – Javascript basics

  • 61k

Var, let & Const are used to declare variables, objects and constants in JavaScript.

Var

Var keyword has been used for a longtime for variable declaration.
It is Function Scoped, it makes variables accessible only within the function boundary where it is declared.
Take a look at this snippet

function getX(){     var x = 1; } getX(); console.log(x)  //ReferenceError: x is not defined  
Enter fullscreen mode Exit fullscreen mode

You can re-declare same variable in same scope with Var.

if (true){     var x = 2;     var x = 3; } console.log(x)  // 3 
Enter fullscreen mode Exit fullscreen mode

Let

Let was introduced in ES6ECMAScript2015.
It is Block Scoped, any code written within {} is said to be in a Block. So, that's the restriction that ECMA wanted to implement with let, making variables inaccessible outside the block.

function exampleLet() {   if (true) {     let y = 20;   }   console.log(y); // Error: y is not defined } 
Enter fullscreen mode Exit fullscreen mode

Also, you cannot re-declare same variable within the same scope.

if (true){     let x = 2;     let x = 3; } // SyntaxError: Identifier 'x' has already been declared 
Enter fullscreen mode Exit fullscreen mode

Const

Const as the name suggests is used to declare Constants.
Declaration with consts are *Block Scoped *
Constant needs to be initialized at the time of declaration, else it throws Syntax error
Re-assigning or re-declaration is not allowed, it throws Syntax or Type Error.
Check out the snippet below

if (true){     const x; } // SyntaxError: Missing initializer in const declaration  if (true){     const x = 1;     x = 2; // TypeError: Assignment to constant variable. }  if (true){     const x = 1;     const x = 2; }  // SyntaxError: Identifier 'x' has already been declared  const x = 2; if (true){     const x = 1; } // Output : x = 2 
Enter fullscreen mode Exit fullscreen mode

Strict Mode

A feature introduced in ES5, it enforces stricter parsing and error handling.

To enable strict mode, you need to put _use strict;_ at the beginning of code or function block.

// Global variable var globalVar = "I'm a global variable";  function baz() {   var localVar = "I'm a local variable";   console.log(localVar); // Accessible inside the function    if (true) {     let blockVar = "I'm a block-scoped variable";     console.log(blockVar); // Accessible inside the block   }   // console.log(blockVar); // Error: blockVar is not defined (not accessible outside the block) } 
Enter fullscreen mode Exit fullscreen mode

Global Variables in Javascript #shorts – YouTube

Javascript tutorial basic 👉🏼 Playlisthttps://youtube.com/playlist?list=PLB4hwRICawVFnTq3T45VbT3ngQkaBy9ZThttps://youtube.com/playlist?list=PLB4hwRICawVFFIe…

favicon youtube.com

javascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.