Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Variables
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. React JS Tutorials
  3. Variables

React JS Tutorial

Modern Javascript Core Concepts

  • Modern Javascript Core Concepts
  • Variables
  • Arrow functions
  • Rest and spread
  • Object and array destructuring
  • Template literals
  • Class
  • Callbacks
  • Promise
  • Async Or Await
  • ES Modules

React Js Basic Fundamentals

  • React JS Introduction
  • How to install React

Variables

Summary: A variable is a literal assigned to an identifier.

A variable is a literal assigned to an identifier.

A variable must be declared before you can use it. There are 3 ways to do this, using var, let or const, and those 3 ways differ in how you can interact with the variable later on.

What is var: 

Until ES2015, var was the only construct available for defining variables.

var x = 0

If you forget to add var you will be assigning a value to an undeclared variable, and the results might vary.

In modern environments, with strict mode enabled, you will get an error. In older environments (or with strict mode disabled) this will simply initialize the variable and assign it to the global object.

If you don’t initialize the variable when you declare it, it will have the undefined value until you assign a value to it.

var x= 'undefined'


You can redeclare the variable many times, overriding it:

var x = 1
var x = 2
  1. A variable initialized with var outside of any function is assigned to the global object, has a global scope and is visible everywhere.
  2. A variable initialized with var inside a function is assigned to that function, it's local and is visible only inside it, just like a function parameter.
  3. Any variable defined in a function with the same name as a global variable takes precedence over the global variable, shadowing it.

What is let:
 

let is a new feature introduced in ES2015 and it's essentially a block scoped version of var. Its scope is limited to the block, statement or expression where it's defined, and all the contained inner blocks.

Modern JavaScript developers might choose to only use let and completely discard the use of var.

If let seems an obscure term, just read let color = 'red' as let the color be red and it all makes much more sense
Defining let outside of any function - contrary to var - does not create a global variable.

What is const:

Variables declared with var or let can be changed later on in the program, and reassigned. Once a const is initialized, its value can never be changed again, and it can't be reassigned to a different value.

const x = 'test const'


We can’t assign a different literal to the a const.

  1. We can however mutate a if it's an object that provides methods that mutate its contents.
  2. const does not provide immutability, just makes sure that the reference can't be changed.
  3. const has block scope, same as let.
  4. Modern JavaScript developers might choose to always use const for variables that don't need to be reassigned later in the program.
Series: React JS Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.