Variables and Data Types In Javascript Programming

Variables and Data Types In Javascript Programming
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.
Variables and Data Types In Javascript Programming
On this matter we will learn about variables and data types that are in Javascript.
We’ll start on the way to create a variable, how to print a variable to output, recharge, and remove it from memory.

How to Create Variables in Javascript

How to create a variable that is commonly used on javascript programming is to use the keyword var followed by a variable name and its value.
Example code:

var title = “Learning Javascript on FajarYusuf.Com “;

In the coding example above, we create a variable named title with a value in the form of text string data type: “Learning Javascript on FajarYusuf.Com “.
Furthermore example code:

var webName = “FajarYusuf.Com“;
var url = ““;
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?
As we Javascript programming is more advisable to use camelCase in naming variables.
Variables and Data Types In Javascript Programming
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;

It will not be a problem or error coding, the program will remain valid and may be in the running. However, the majority of Javascript programmers prefer to use camelCase.
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 .
Example code:

// 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).
Example :

var x;

Then the variable x will be worth undefined.

How to display the contents of variables Javascript

How to display the contents of variables, we can use existing functions for output like the following:
  • The function console.log () output to the console javascript;
  • 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   HERE  :
<html lang="en">
    <title>Belajar Variabel Javascript di FajarYusuf.Comtitle>
        // membuat variabel
        var name = "FajarYusuf.Com";
        var visitorCount = 1404;
        var isActive = true;
        var url = "";
        // menampilkan variabel ke jendela dialog (alert)
        alert("Selamat datang di " + name);
        // menampilkan variabel ke dalam HTML
        document.write("Nama Situs: " + name + "
        document.write("Jumlah Pengunjung: " + visitorCount + "
        document.write("Status Aktif: " + isActive + "
        document.write("Alamat URL: " + url + "
The result:
Variables and Data Types In Javascript Programming
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 in javascript is rarely done. However, for programs that require precision in memory allocation, elimination of variables necessary for optimal memory usage and can be controlled.
Elimination of variables can be done with the keyword delete.
Example code:

title = “Javascript Programming Study In FajarYusuf.Com “;
delete title;

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.
There are several types of data in Javascript programming:
  • String (text)
  • Integer or Number (integer)
  • Float (number fraction)
  • Boolean
  • Object
Javascript is a language that is dynamic typing , meaning we do not have to write or specify the data types when creating variables. Unlike in C, C ++, Java, etc is static typing .
Example code:

var name = “Eka”;
var age = 24;
var single = false;

Javascript will automatically identify the type of data that we give to each variable by value.
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 .
Example code:

typeof name;
typeof age;
typeof single;

The results on the console:
Variables and Data Types In Javascript Programming
How we can identify the type of data on javascript in addition to using typeof?
We can recognize by way of writing.
Example code:

// 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;

Rules For Writing Variable Name in Javascript

There are a few rules of writing variables in Javascript:
The first rule: Naming variables can not use the numbers in front of .
example code:

// wrong
var 123name = “FajarYusuf.Com“;

// right
var name123 = “FajarYusuf.Com“;

Rule Two: Naming variables can use the initial underscore.
coding examples:

var _name = “FajarYusuf.Com“;

Third rule: Naming variables when recommended use CamelCase consisting of two syllables.
Coding example:

var webName = “FajarYusuf.Com“;

Fourth rule: Naming variables is recommended to use English
Example code:
var postTitle = “Tutorial Javascript on FajarYusuf.Com“;

Leave a Reply

Your email address will not be published. Required fields are marked *