Sign in
Log inSign up

Variables declaration and its values

Adebimpe Adewole's photo
Adebimpe Adewole
·Dec 20, 2020·

5 min read

As you have opened this link, do me a favour to read this article to the end. And when you read all the way down leave a comment, like and share. In this article, I will be writing everything I understand about variables using part of data types like string and number. Let’s dive right into it.

A variable
A variable is a name that refers to a keyword declaration (Var, let, and const) which refer to a particular value. A variable can be created by first writing the Var, let, const keyword and thereby give it any variable name you choose to give it. For instance:

image.png

Now a variable has been declared and a value is assigned to it. I choose to give it (name) as my variable name and I assigned (John) which is a name of a person and end it with a semi-colon. If you look closely on the value assigned to the variable which is (john) you would notice that I have enclosed the value inside with a double quote (in JavaScript is called a String). In javaScript when you assign a value that is not a number, object, an array and any other data types, the value has to be written inside a string (double quotes) so that when you call the name of the variable then you would get the expected result which is John in line 2 in this case. There might be some times you would forget to add a string to the value you assign to your variable.

For instance when you write a code like this

image.png

You would get an error of undefined. You will have to check your code declaration if you do what is expected of the code to run. When you invoke the name of this variable (name) is bringing undefined because the name is seeing the value as a variable that is not initially declared and because of that it brings the result as undefined. The above code can only run when a string is applied to it. Don’t do this mistake so that you will not say your code is not working hence the reason for all this story . Going forward, as I have stated earlier that other data types like an object, arrays, numbers and so the rest can also be assigned to a variable to make a statement in javaScript.

image.png

In the example above, a variable name called (num) is defined on the left side. Num is just a name I choose to name my variable name, remember I said you can call it any name you like. On the right side, a number (38) is assigned to the variable without any string surrounding it. When the variable name is invoked, 38 would be executed as the expected result. Trying to adjoining a string around a number would be executed and display a number as its result. The only way to know if the result is a number is to use (typeof) with the variable name, and the value would result in a string. See the example below:

image.png

The code above explains how to know if the value assigns to (num) variable is either a number or a string.

Thank you for reading. I hope you gain more knowledge on one or two things in this article. Kindly like, share and do well to drop a comment. Cheers