Variables and Data Types are two basic things in a programming language is always used, and these two things will always exist in any programming language.
What definition of variables?
variable is the name that represents the value. Variables can be filled with a wide variety of grades with different types of data, such as strings (text), number (digit), objects, arrays, and others.
If compared with an object, then that variable as a container for storing things.
We'll start on the way to create a variable, how to print a variable to output, recharge, and remove it from memory.
Furthermore example code:
var webName = "FajarYusuf.Com";
var url = "https://www.FajarYusuf.com";
var visitorCount = 1404;
In the example above, we use letters to major variable names consisting of two syllables.
Underscore why we do not use it?
If we can not use camelCase? , Can only use a case or underscore snake like this example:
var web_name= "FajarYusuf.Com";
var visitor_count = 1404;
According to your taste, want to join the camelCase majority or snake_case minority.
It is necessary to note also, in addition to using the keyword var can also create a variable with the keyword let or without any keywords .
// create a variable with the keyword let
let price = 14000;
// create a variable without any keyword
stock = 12
What is the difference between the three ways that using var, let and without the keyword?
The difference lies in the reach and use. Will be discussed more deeply.
Other questions might be:
"What happens if the value of the variable is not filled?"
The answer, it would be worth undefined variables (undetermined).
Then the variable x will be worth undefined.
How to display the contents of variables, we can use existing functions for output like the following:
- Function document.write () output to the HTML document;
- and Function alert () output to the dialog window.
Let's try it, create a file called learning-variabel.html, then fill with the code below, or you can also try the html editor fajaryusuf.com HERE :
var name = "FajarYusuf.Com";
var visitorCount = 1404;
var isActive = true;
var url = "https://www.fajaryusuf.com";
alert("Selamat datang di " + name);
document.write("Nama Situs: " + name + "
document.write("Jumlah Pengunjung: " + visitorCount + "
document.write("Status Aktif: " + isActive + "
document.write("Alamat URL: " + url + "
The meaning of the plus symbol ( + ) in the above example is to combine, not to add up.
How to Charge Changing the Content of Variables
Variables are mutable, meaning that the value stored in it can be refilled or changed.
Example code :
// first we create a variable with the contents of this early
var age = 25;
// then we change it
age = 17;
Why when recharging or change the value of the variable does not use the var keyword?
Because the var keyword is required when creating a variable. As for recharge, we just write as above.
If we use the var keyword, meaning it can be interpreted we create a new variable, not recharge or change the value.
How to Delete Variable
Elimination of variables can be done with the keyword delete.
Then the variable title will be erased from memory.
Elimination of variables can only be performed on variables that are made without the prefix keyword. While the variables created with the keyword var and let will be deleted automatically.
What Is a Data Type
The data type is various types of data that we can use in variable to specify the data type of the value.
- String (text)
- Integer or Number (integer)
- Float (number fraction)
var name = "Eka";
var age = 24;
var single = false;
In the above example, we would get the following:
- String data type name;
- age integer data type;
- and single boolean data type.
For certain things from these data types we can also check with the keyword typeof .
The results on the console:
We can recognize by way of writing.
// for text or string data type of writing is always enclosed in quotation marks
var author = "Fajar Yusuf";
// while the number and Boolean data types are not enclosed in quotation marks
var available = true; // for data type boolean only true and false
var total = 25;
The first rule: Naming variables can not use the numbers in front of .
Rule Two: Naming variables can use the initial underscore.
Third rule: Naming variables when recommended use CamelCase consisting of two syllables.
Fourth rule: Naming variables is recommended to use English