Responsive Advertisement

How do you declare a JavaScript Variable and Values

Declare a JavaScript Variable and Values

How do you declare a JavaScript Variable and Values?

A value is basically a piece of data. It is the most fundamental unit of information that we have in programming. For example, look at the below code that I provide as an example. The 1st line text is "Akash", which is a value. And if we want to actually see this in the console, follow the 2nd line of code. The 3rd line value is 30. It is also a value. We can see in the output that the 2nd and 3rd lines are printed on the console (Please see the output image below).

How do you declare a JavaScript Variable and Values
Output

One extremely useful thing that we can do with values is to store them into variables. This way, we can reuse them over and over again. For example, see line no. 4. There we take a variable with let and then the name of the variable is firstName with an assigned value like "Touhid". Ohh!, If you don't know how to declare a variable in JavaScript, please find out the other articles on this website or write it by yourself. But I will describe some details about these to make sense of it. We can declare variables in three ways in JavaScript. which is var, let, and const. Simply put this before the variable name. I hope you get a sense of this thing. Back to the main thing we did here, which is called declaring a variable, which is "firstName." This will actually create a real variable in your computer's memory, and we will store the value inside of that variable.

So I like to imagine a variable like a box. In the real world, a box can hold some objects, for example, a book or something like this that you imagine. We can write a label on the box to describe or find what is in it. With this, we can easily find the object later when we need it by using the label. The variables actually work in the exact same way. So here, basically, we have a box called "firstName" and in the box, we put the value of "Touhid" as the object.

In other words, if we want to use this value, we have to use this label, in other words, the variable name "firstName." Let's actually use it. Once again, I will use console.log in line 5. Before console.log, we used the value directly. But in this case, we will use the variable name "fistName" to display the value in the console in the web browser. We can see the output image above, which I provide as an example. If you change the value of the variable "firstName" to anything, it should be shown in the browser console that you assign.

So let's just copy line number 5 and paste it below to lines number 6 and 7. For this, we have it three times in the browser console. That means that whenever JavaScript sees the "fistName" variable name, it will basically replace it on lines no. 6 and 7. It basically replaces the original value that we assigned to the variable with the "fistName" variable in line no. 5. Now if I want to change the variable "fistName" value to something else, the three-time console shows the same thing in our browser console output. 

This is one of the big advantages of variables in JavaScript. Without variables, we need to change the value manually everywhere to "Touhid." But like this, everywhere where I reference the "firstName" variable, it will automatically change into anything that we specified in the variable. That is one of the most important things to keep in mind about variables.

Now we know what a variable actually is. Let's just very quickly talk about conventions and rules for naming variables. Basically, we can assign any name to a variable. I mean anything you want. But as a professional programmer or developer, you abide by or follow some rules for naming a variable. Now we should not just give random names to variables. So first, the "firstName" variable that I declared in line 5 is called camelCase. When I have multiple words in a variable name, I use camelCase to write the first word in lowercase and all subsequent words in uppercase, as in the "firstName" variable. Here I wrote the "first" word in lowercase and the last word, "Name," in uppercase. This is the one kind of standard in the JavaScript world. I hope you understand what I want to say.

But of course, you have the other way to name variables. For example, we could write "first_name" with an underscore like inline no. 8. This is very popular in other programming languages. You can use whatever you like most. Just keep in mind that it is kind of a standard in JavaScript to write a variable named camelCase. So usually, whenever you see another person's JavaScript code, the variables will usually be written using the camelCase notation. So this is kind of a convention for how to name variables in JavaScript. But there are also some actual hard rules in JavaScript about how we can name variables. For example, we cannot name a variable with a number or start words with a number like "1stName" or "5512". If you write a variable like this, you will get an error in our browser. I can display the invalid or unexpected token, which is obviously SynTaxError. means we made a mistake in writing our code. So that's a mistake in the code's syntax. All the errors are automatically shown in the console. Remember this carefully. Otherwise, you wrote a thousand lines of code but made a mistake in a certain line like this or assign a variable name starting with a number. Your entire code is not working. 

So we already know that variable names can not start with a number or variable names can not be the only number. In fact, variable names can only contain numbers, letters, underscores, or the dollar sign. Keep in mind these things because others are not acceptable in JavaScript for variable names. Another error might occur when we try to name a variable using a reserved JavaScript keyword. For example, "new" or "function" keyword If you want to know more about the JavaScript reserved keyword, just search on Google with "JavaScript reserved keyword". You will see all the reserved keywords.

Another convention is that we should not start a variable name with an uppercase letter. For example, I wrote a variable with the first letter in uppercase, like "Person" in line no. 9. But it works properly, as we see in the browser console. Now again, it is a convention, so that is not illegal. It is just that we use these kinds of variable names with an uppercase letter for a specific use case in JavaScript, which is object-oriented programming. The variables that are all in uppercase are reserved for constants that we know will never change. For example, the number of PI is known to be a constant. See the example of line no. 11. We know the PI value is 3.1415 like this. So we know that this number is never going to change, and so that is a constant. For that, we have a convention of writing it in all caps. So if it is a real constant, write it in uppercase like line number 11. This is a pretty normal convention in programming as a whole.

The final convention is to make sure that our variable names are descriptive, which is very important for writing cleaner code. So, when we name a variable, reading the name of the variable should make it very clear what value the variable holds. That is kind of what we did in line 4 by calling this variable "firstName". For this, we can easily understand that the variable show holds or keeps a person's first name. Let me show you another example in lines 13 to 16. I named the first variable myFirstJob and the second myCurrentJob, as well as lines 15 and 16. I label the other variable with job1 and job2, and the variable has the same values in both cases. Now, let's say that the variable name is better for understanding their values. Of course, in lines No. 13 and 14 Here we can easily guess the person's first job from line 13's variable name and current job from line 14's variable. But on the other hand, lines no. 15 and 16 variable contain just two variables. which is just two job listings. Nobody understands the difference between a person's first job and their current job.

So keep that in mind whenever you write your own variable names and actually keep all of this in mind that I described as the avobe for naming variables. After that, if you have any questions about JavaScript values and variables, just comment below. I will try to explain your question or variable.

Post a Comment

0 Comments