1ce27d31b7245e94107090a087947ad5d1ac0f51d33c32d1436ffb03f085db7374f17ba94ceece57

Basic JavaScript Tutorial: Variables and Data - Part 2

Maret 29, 2020 By: Rahman N
If we make a task in Word form for example, then we have to save it so we can use it again. This is also very important in writing a code. Luckily, JavaScript can do it too! If not, maybe I don't think we need to use and learn JavaScript.

Now, let's ask someone's full name. For example, here I will ask your names.

Oh yes, before continuing, make sure you have opened the Console in the browser to run the sample code below.

var fullname = prompt ('Hello friend, may I know you, what is your name?');
The prompt () method can be used to display a dialog or pop up message asking the user to write text into the input.
When you have written the code above in the Console, a Pop-Up Box will appear asking "Hello friend, may I know you, what is your name?" There you can write your name and press the "OK" button.

The full name that you have entered is now stored in the browser, and remember that your name is stored in a variable called fullname. You can see again what you have entered in the Pop-Up box by typing the full name into the Console. If so, you will see the results. Cool is not it?

Well, here you have succeeded in creating a variable and the name of the variable that you have created is the full name.

Variabel

Imagine if you want to put a book on a bookshelf. In the previous example you created a variable named fullname . So, the variable named full name can be likened to a bookshelf that functions to store data.

You can create a variable name with the name you want. However, the best variable name is what is related to the thing stored in it. Example: If you want to save a book's name in a variable, then create a variable with the name Book name.

When you type or enter variable names into the Console, that means you are asking the browser to search for a bookshelf and find the name of the book you are looking for. In JavaScript, the name of the book you are looking for is called Value. Values ​​in the form of letters are called strings. In addition, you can also store Values ​​in the form of numbers and other data.

So, a variable must have a name and a value,

That's our way to store data in JavaScript, and I'm sure you will often use it.

There are two ways to create a variable, namely Declaration and Initialization. When a variable has been created, you can set / set its value.

Declaration

Declaration (Declaration) is an action to determine a variable name. Now, like the example that we discussed above, to determine or declare a variable, we must use the Keyword var followed by the variable name. Example:
var fullname;
var age;
Note: Note that there is a semicolon (;) at the end of the variable name. Each end of the line in JavaScript code must have this sign.
Initialization
Initialization is an action to assign a value to a variable.

You can assign values ​​to variables by using the equal sign (=). In other words "The value or data of the variable is on the right side of the equal sign". Example:
var name='Codesafe';
In JavaScript, "Codesafe" is called a String - it's because the values ​​of the variables are letters. A String must be surrounded by quotation marks one (') or quotation marks two (").

Then, what about the following example?
var age= 19;
19 Just a number - A variable value in the form of numbers does not need to be surrounded with any sign.

Determination
As we learned above, you can add as many variables as you want. This is called Assignment and can look the same as Initialization. Again you must use the equal sign (=). But the difference is, you don't need to set Keyword var anymore because you have already set a variable.

For example like this:
name ='Codesafe';
umur = 19;
Note: If you want to make a determination, make sure that you have previously specified the variable name (Keyword var).
View: - Tags: Javascript