Software

JavaScript Variables

Variable identification

Rate this post

In JavaScript, data structures created to temporarily store data are defined as variables. Another one
It can also be called rhetorically named memory.

JavaScript has variable declarations and is generated with the let keywords.

var y;
y=7;
document.write(y);

Here document.write(y) means print the code to the screen. The variable in parentheses is printed to the screen.

We can define and initialize the variable in the same line.

Variable Identification:

  •  All variable names must be different from each other.
  •  JavaScript variable names are expressed as identifiers.
  • Identifiers can be short such as x and y or more descriptive such as Place of birth, name and surname, xNumber.

JavaScript Variable Declaration Rules:

  •  Letters, numbers, dollars and underscores can be used when defining a variable.
  •  Variables can begin with letters, underscores, or dollar signs.
  •  Variable names are case sensitive. For example, Y and y are different variables.
  • No spaces are used between variable names.
  • JavaScript keywords cannot be used as variable names. For example var, if, while…etc.

Value Assignment:

The equal (=) operator is used to assign any value to the variables defined with JavaScript.

var y = 5;

Variable Types:

JavaScript variables can take numeric values such as 1788, as well as textual values such as sydney.

Numeric values are written without quotes, while textual values are written in single or double quotes. If numeric
If values are enclosed in quotes, JavaScript will treat the variable as a textual value.

var pi = 3.14;
var name = “yaska”;
var hello = “Hello JavaScript”;

Creating A Variable:

JavaScript variable declaration is used with the var keyword.

var name;

The above variable has no value. The value of our variable is defined as undefined. Value to variable
We should use the equal “=” operator for our assignment.

name = “yaska”;

We can also assign a value to a variable while creating a variable. Let’s show it with an example:

 

var name = “yaska”;

 

Here, the variable has been created, the yaska value has been assigned and it is written on the screen as the alert notification panel.

<script>
var name = “yaska”;
alert(name);
</script>

 

Defining Multiple Variables:

More than one variable can be defined and it will be enough to put a comma after the variable name.
<script>
var name = “yaska”, fo =”Hello JavaScript”, xnumber = 1788;
alert(name);
</script>

We’ve defined multiple variables, if we think it’s confusing, we can make it more readable and suddenly
We can split it into multiple lines.

<script>
var name = “yaska”,
fo = “yaska”,
xnumber = 1788;
alert(name);
<script>

Undefined Variables:

If the value of the variable is the result of the calculation or if the user will get the value after the data entry, usually the variables are valued.

If it is created without assignment and without giving a value, it takes the undefined value ”undefined”.In the example, a variable has been created as “name”, but since no value has been assigned, it has taken the value of undefined.

<script>
var name;
alert(name);
</script>

Recreating The Variables:

The JavaScript defined variable retains its value if recreated.

<script>
var name = “yaska”;
var name;
alert(name);

</script>

NOTE: If a value is assigned to a re-created variable, the newly assigned value will be valid.

Variable Arithmetic:

We can use arithmetic operators on variables.

<script>
var xNumber = 7 + 1 +2;
alert(xNumber);
</script>

Arithmetic operators will be treated as concatenation operators if we use them as textual values.

<script>
var name = “yaska” + “fo”;
alert(name);
</script>

If we put the numbers in quotes, it will work as a textual concatenation operator.

<script>
var xNumber = “7” + 6 + 1;
alert(xNumber);
</script>

The following will be added to 8 and 4 first, then as the variable concatenation operator because the value is written as text.
it will work.

<script>
var xSayisi = 8 + 4 + “3”;
alert(xSayisi);
</script>

Related Articles

Leave a Reply

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

Back to top button