DEV Community

Cover image for Javascript Chapter 3 - Knowing Variables in JavaScript
Dzun N
Dzun N

Posted on • Updated on

Javascript Chapter 3 - Knowing Variables in JavaScript

Not only in PHP we can find variables called variables, but also JavaScript has variables. The use of variables in javascript is the same as the use of variables in other programming languages. Namely for temporary data storage.

HOW TO WRITE VARIABLES JAVASCRIPT

What must be considered in writing javascript is, all writing variables in javascript must not contain spaces, must pay attention to writing upper and lowercase letters. For more details, consider the following points.

An important point in writing javascript variables

  • Writing variable names can use letters, numbers, underscores, and dollar signs
  • Writing variable names must begin with a letter
  • Writing javascript variable names must pay attention to uppercase (case sensitive)
  • Variable names cannot contain spaces
  • Writing javascript variables begins with the syntax "var"

HOW TO MAKE A JAVASCRIPT VARIABLES

Well, first we prepare the html file, here I named it index.html

index.html

result index.html

As we saw in the syntax above. I created two variables which I named "nama" and "alamat". Don't forget to put a semicolon or semicolon (;) at the end of the variable line as this is a definition of javascript.

var nama = "Nurroin's Blog";
var alamat = "Probolinggo";
Enter fullscreen mode Exit fullscreen mode

And then I output the contents of the variable into the nama and alamat elements

document.getElementById("nama").innerHTML = nama;
document.getElementById("alamat").innerHTML = alamat;
Enter fullscreen mode Exit fullscreen mode

Note : document.getElementById() useful for specifying element by id in accordance with the function parameter. innerHTML to implement HTML syntax.

CONCLUSION

Variant is a place to store temporary data and the contents of this variable can be retrieved. There are several important points that we should know when using variables in javascript. That is case sensitive, we must pay attention to the upper and lower case letters that we make, we must not precede variable names with letters, etc. as I explained above.

Top comments (0)