JavaScript Variable Definition

As in any programming language, we need to define variables depending on what we do, and we can use these variables directly in their name when we use them in the code. The first thing we need to know in the JavaScript variable definition process is the variable definition rules.

 

There are some rules to consider when defining variables. The variables we have defined can be a mathematical operation or a classic character. It is entirely up to you to use the variable that you have defined and assigned or not.

 

How to define a JavaScript Variable ?

As we mentioned above, first of all we need to know will be variable definition rules. For JavaScript variable definition rules, we need to know:

  • When defining a variable, variable names can contain letters, numbers, underscores, and dollars.
  • As I mentioned above, variable names can start with letters, numbers, underscores, and dollars.
  • Variable names are case-sensitive. You have to watch out for this.
  • When defining a variable name, the name must not contain spaces.
  • No JavaScript tags are used when defining a variable name.
  • The variable name must not contain Turkish characters when defining the name.

 

Now we have learned the above points to consider when performing JavaScript variable definition. After that, let’s talk about how to define variables in JavaScript.

 

<!DOCTYPE html>
<head>
</head>
<body>
<h1>JavaScript Variable Definition</h1>
<p id="demo"></p>
<script>
var x = 5;
var y = 10;
var z = x+y;
document.getElementById("demo").innerHTML =
"The value of Z is x + y" +z;
</script>
</body>
</html>

In the example above, we now learn to use var before defining variables. Before defining the variable, we present the name of the variable. As you see it as a variable, we may even specify something as a mathematical operation. But if we are a method of defining a variable that is different from the other instead of writing the var command:

 

<!DOCTYPE html>
<head>
</head>
<body>
<h1>JavaScript Variable Definition</h1>
<p id="demo"></p>
<script>
var person = "Gokmen" + " " + "Efsun" ,car ="Volvo", money=500;
document.getElementById("demo").innerHTML = person+car+money;
</script>
</body>
</html>

In this example, we have specified that we will define a variable using only one var command. After that, for each variable we define, we can put a comma and then write the name of the variable. On the other hand, when I write something with the plus operator, which is the merge operator, as you have seen, I can make it print by leaving a space.

 

If we are going to add the situation, the most important things we need to pay attention to are to obey the variable definition rules and assign something we want into the variable we define. These can be a character and many more in mathematical operations. In this regard, you can specify the value of the variable in the line above or in the line below and use the variables as you would like.

Leave a Reply

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