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 visitorCount = 1404;
var isActive = true;
var url = "https://www.fajaryusuf.com";
// menampilkan variabel ke jendela dialog (alert)
alert("Selamat datang di " + name);
// menampilkan variabel ke dalam HTMLdocument.write("Nama Situs: " + name + "<br>");
document.write("Jumlah Pengunjung: " + visitorCount + "<br>");
document.write("Status Aktif: " + isActive + "<br>");
document.write("Alamat URL: " + url + "<br>");
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.
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 .
typeof name; typeof age; typeof single;
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 .