When working with code, data is information you can use for your programs. For instance, your Facebook username is called a piece of data. More programming is to stimulate or represent data.
The developers should find a solution for storing and monitoring the data to do it. Let’s follow us to clarify it with the example below.
Ctrl + Shift + J on operating systems (Linux and Windows). You can get “
Cmd + Option + J” regarding the Mac platform.
When the console has run, suppose about the pet, like the dog’s current age (or any number if you do not own any pets), and type it in your console.
Currently, what if you need to display this number again? You must type it out on another try. In this case, you have to find a good approach to involve in that data piece so you can use it again in your project.
A beneficial similarity is to discuss variables as labels for the values. Suppose the container of strawberries with the label is assigned strawberries. As for this case, the variable, like strawberries, targets the value that is the strawberries themselves.
Next, you have to declare the variable: age and apply the assignment operator to set the value: 34 to this variable. In other words, you must use the var keyword like below.
var numAge = 34
At this moment, you have set this value to the variable age and might involve back to it soon. If you currently type in the variable age in the console, you can return the value: 34.
Next, prove it by initially declaring a new variable and name. Next, you assign it to the value: ‘
var fullname = 'Tiffdoan'
Afterward, you must assign this variable again to target the various name values like ‘Tiff’.
fullname = 'Tiff'
Currently, if you start running the
console.log(fullname), you might obtain the result of Tiff. When you use the var keyword, variables might even be declared without a previous value.
Next, declaring a variable ‘year’ might not point to the value. In this case, if you need to point the value to it, we will suggest that you might use the assignment operator to deal.
year = 2022
A tip of declaring variables without using the var keyword
Variables might be declared and start without the var keyword. However, the value should be set to the variable declared without the var keyword. As usual, the variables declared without the var keyword will be the global variables.
Besides, it is not highly suggested to declare the variable without the var keyword since it could overwrite the current variable accidentally.
Message received: Wellcome to LearnshareIT.com
The scope of the variable is the program’s region where it is assigned. The variables offer you double scopes like below.
1. Local variables
A local variable might be visible only with the function in which it is assigned. Next, the function parameters are often local to this function.
2. Global variables
A global variable provides a global scope that demonstrates it might be assigned anywhere in the JS code.
On top of it, from the function’s body, the local variable gets precedence over the global variable with a similar name. If you use the local variable with a similar name to the global variable, you can hide the global variable easily.
Now, let’s see the illustrated example below.
It generates the outcome below.
Hello Tiff Doan
When you name the variables in JS, please conform to the rules below.
- You might not take any JS reserved keywords as the variable name. For instance, boolean or break variable names are invalid.
Java or C# has included the typed variables. In other words, a variable will be declared with the data type, which defines the data type the variable can store. Besides, JS is a loosely typed language.
This demonstrates that it might not ask the data type to be declared. You could assign the literal values to the variable like float, integer, string, and so on.
let testVar = 9; // numeric console.log("testVar: ", testVar); // decimal testVar = 22.9; console.log("testVar: ", testVar); // Boolean testVar = true; console.log("testVar: ", testVar); // string testVar = 'Tiff'; console.log("testVar: ", testVar); // NaN testVar = NaN; console.log("testVar: ", testVar); // null testVar = null; console.log("testVar: ", testVar); // Hexadecimal testVar = 0xFF; console.log("testVar: ", testVar);
testVar: 9 testVar: 22.9 testVar: true testVar: Tiff testVar: NaN testVar: null testVar: 255
Some articles related to the topic of JS Variables that you may be interested in:
Hi guys, wellcome to LearnShareIt. I’m Tiffany and I’m also who responsible for the quality of this website’s content. I previously worked on software development projects for ten years as a developer. Hopefully, our project will bring a lot of value to the community.
Job: Programmer/Project management