DEV Community

Cover image for Understanding JavaScript Variables Before Blaming React
Kunal
Kunal

Posted on

Understanding JavaScript Variables Before Blaming React

JavaScript looks easy at first until you realize everything depend on variable and Data types. If you skip these fundamentals your code quickly turns into the guessing game and you end up console.log everything.

Topics to Cover

  • What variables are and why they are needed
  • How to declare variables using var, let, and const
  • Primitive data types (string, number, boolean, null, undefined)
  • Basic difference between var, let, and const
  • What is scope (very beginner-friendly explanation)

Variables

When a child is born they are given a name and throughout their life they will be referred by that name unless the name gets changed.

In JavaScript values are assigned by a name and anytime we need that value we simply mention the name to which it was assigned.

Before declaring or assigning a variable the first step is learning how to name it correctly.

πŸ‘‰ How to Name Variables

Just like human we always put much thoughts to ensure the name has a meaning to it. We do it same for JavaScript.

for example pneumonoultramicroscopicsilicovolcanoconiosis is a valid name but it too long.

Let your variable be simple and context full. For example clickButton , addNumbers , sumOfNumbers

It should be self explanatory.

Now lets see how to handle a variable

πŸ‘‰ How to declare and assign a variable

To declare the variable you can use one of these keywords: let , const or var

let count = 1
Enter fullscreen mode Exit fullscreen mode

We can assign a value to a variable by using the assignment (=) operator. This mean that we provided an value for the variable when it was created.

Some names are already used by JavaScript to mean something these names cannot be used by developers.they are called reserved words check this πŸ‘‡

πŸ‘‰ How to Call a Variable

Once a variable has been created and assigned a value we often need to access or display that value in our program.

In JavaScript one common way to do this by using console.log() which prints it.

console.log(count + 2)
Enter fullscreen mode Exit fullscreen mode

In next section we will learn about different data types and how to work on them


Data Types

In JavaScript , Datatypes defines what kind of value you can hold inside a variable.
Think of data types like different types of containers in real life :

  • A water bottle holds water
  • A wallet holds money
  • A folder holds document

The data types in JavaScript are categorized into two primary groups namely :

  • Primitive : Number, String, Boolean, Undefined, Null, BigInt, Symbol
  • Reference : Object, Array, Function

Let's consider primitive data types.

Primitive Data Types

1. String :- Text inside quotes

eg:- let name = "Like this blog"
Enter fullscreen mode Exit fullscreen mode

2. Boolean :- Only two values

eg:- True , False 
Enter fullscreen mode Exit fullscreen mode

3. BigInt :- For very very large numbers.

eg:- let bigNumber = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

4. Number :- Any normal number.

eg:- let marks = 95;
     let price = 99.99;

Enter fullscreen mode Exit fullscreen mode

5. Undefined :- When a variable and no value is given

eg:- let x;
Enter fullscreen mode Exit fullscreen mode

6. Symbol :- Creates a unique value.

eg:- let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

7. Null :- Means intentionally empty.

eg:- let data = null;
Enter fullscreen mode Exit fullscreen mode

8. Object :- Stores multiple values together

eg:-  let user = {
  name: "Kunal",
  age: 21
};
Enter fullscreen mode Exit fullscreen mode

Note :- In JavaScript datatype of Array is object.


Difference between var, let, and const

Feature var let const
Scope var works inside a function. let works inside a block { }. const works inside a block { }.
Redeclaration You can declare the same variable again. You cannot declare the same variable again. You cannot declare the same variable again.
Change value The value can be changed later. The value can be changed later. The value cannot be changed later.
Initialization You can declare it without giving a value. You can declare it without giving a value. You must give a value when declaring it.
Modern usage Mostly avoided in modern JavaScript. Commonly used when values change. Commonly used for constant values.

Now that we’ve seen the differences between var, let, and const, let’s understand scope.
Scope simply means where a variable is accessible in your code.


Scope

Scope defines the area of the code where a variable can be used or accessed.

var has function scope, which means it can only be accessed inside the function where it is declared.

function example() {
  var message = "Hello";

  console.log(message); // Works here
}

example();

console.log(message); // Error: message is not defined
Enter fullscreen mode Exit fullscreen mode

let has block scope, so it can only be accessed inside the block { } where it is declared.

{
  let age = 25;
  console.log(age); // Works here
}

console.log(age); // Error: age is not defined
Enter fullscreen mode Exit fullscreen mode

const also has block scope, meaning it can only be used inside the block { } where it is defined.

{
  const PI = 3.14;
  console.log(PI); // Works here
}

console.log(PI); // Error: PI is not defined
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ! if enjoyed this blog , you can read more on this πŸ‘‡

Top comments (0)