fbpx

How to Declare Javascript Variables: Javascript Tutorial6 min read

[lmt-post-modified-info]
Declaring JavaScript Variables
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp
Share on email

Table of Contents

Hello and welcome to this JavaScript tutorial. Today we are declaring JavaScript Variables.

Javascript is a very important language in web development, it is very popular in the development world with various frameworks. Learning the basics such as this is very crucial.

Declaring JavaScript variables allow us to store data so we can use it again and again. This is almost like algebraic expression in mathematics where we have something like the below expression

x=5;

Calculate x+ 2.

To declare JavaScript variables we use the var, let or const keyword. The assignment operator (=) is for assigning values or the data type you want to store either string, number, etc. As shown below:

First, we will declare a variable with the name newVariable:

declaring JavaScript variables

Then, we put data into it using the assignment operator:

Note: Assigning data to a variable also means we initialize the variable

We can also declare two and even more variables in a line:

Now, we have the gist about declaring variables, right? Let’s now look at the differences between the keywords var, let and const. Yes they pretty much have the same function of storing data but they are different in their approach to storing data; as we will see below:

VAR

This is probably the most popular JavaScript variable declaration reserved keyword. The var keyword has been available since JavaScript’s initial release.

When we use var to declare JavaScript variables in a function, it becomes functional Scoped. It becomes global scoped when declared outside a function. The code below demonstrate this:

Declaring variables with var
Declaring variables with var

Now for an example of a global scoped variable:

Global scoped variables in javascript
Global scoped variables in javascript

Also , var declared variables can be re-declared and updated within its scope.

LET

The let keyword arrived with modern JavaScript.

Unlike the var keyword where variables are either in local or global scope, let variables however are in block-scope – they are limited to the expression or block in which they are defined. This is further explained below:

This however, will work:

Valid ways of declaring javascript variables with let

A block statement is everything inside { }, (for example, an if condition or loop).Therefore, let keyword can help reduce error while coding.

CONST

The const keyword is another way to declare JavaScript variables.  We use the const keyword like the var and let keyword but it is to define variables whose values will never change – unchanging or constant values. This means that when you use const keyword the data store in it becomes constant and cannot be re-declared or updated.

For example:

Declaring variables with const
Declaring javascript variables with const

Const declared variables are also block scoped like the let keyword.

When you are sure your variable will never change, then declare it with const. Most programmers do this.

Note: You can put different data types even function in JavaScript variables.

data types for JavaScript variables

Naming your JavaScript Variables

Yes, it is very important to name your variables right.

Naming your variables right help other coders to read the code well. When you also come back to the code, you will not have problems reading it.

So when you declare variables, think about the right name for the variables.

JavaScript info have some good rules to follow in naming JavaScript variables.

Also, there are rules to naming JavaScript variables, such as:

  1. The name must contain only letters, digits, or the symbols $ and _.
  2. The first character must not be a digit.
  3. When the name contains multiple words, camelCase is commonly used.
  4. Case matters when naming variables. Variables named firstNumber and firSTNumber are different variables.
  5.  Reserved words cannot be used as variable names because they are used by the language itself.
Valid ways of naming JavaScript variables
Valid Ways of Naming JavaScript Variables

Scaling up your business or learning new things can seem daunting but it is quite easy, Checkout these wonderful posts for inspiration:

Invalid way for JavaScript Variables naming
Invalid Ways of Naming JavaScript Variables

CONCLUSION

Let’s recap, shall we?

So I have an awfully long mathematical question to solve and I don’t want to be rewriting the value every time, I will declare JavaScript variables to store my data or values- just once. This makes it easy for me to continue my coding without frustration.

So yeah, get into the habit of declaring JavaScript variables, You’ll be happy you did.

Also, some advised that in other to reduce errors you should use const and let keyword whenever possible. Of course, you can use var too but only when needed.

That’s all for JavaScript tutorial today, go and declare JavaScript variables away.

Rasheedat Usman

Rasheedat Usman

Rasheedat Usman is a web and ecommerce developer at Fovero Digital Technologies. She has great expertise in front-end web development and strives to learn and explore more, the world of technology.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Business Empowerment Program

Join Our Business Empowerment Program

Get a website and more from #10,000 only.
Get started now!

Don’t forget to share on Social Media!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp
Share on email
Grow your skills

Hey dear, just Before You Leave!

Get Updates To Help You Grow.

We offer more than just services but teach people what we know to help them grow FOR FREE.

We promise not to spam!

Don’t Forget to check our blog/resources hub.