Categories
JavaScript Web development

JavaScript Variables

Variable means anything that can vary. JavaScript variables are containers for storing data values and it can be changed anytime.

JavaScript Variables

Example1 – In this example, x, y and z, are variables:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo"></p>

<script>
var x = 6;
var y = 4;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>

</body>
</html>

Output:

Example2 – In this example, price1, price2, and total, are variables:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo"></p>

<script>
var price1 = 6;
var price2 = 4;
var total = price1 + price2;
document.getElementById("demo").innerHTML =
"The total is: " + total;
</script>

</body>
</html>

Output:

In programming, just like in algebra, we use variables in expressions (total = price1 + price2).
From the example above, you can calculate the total to be 10.

JavaScript Identifiers

All JavaScript variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

The general rules for constructing names for variables (unique identifiers) are:

  1. Names can contain letters, digits, underscores, and dollar signs.
  2. Names must begin with a letter
  3. Names can also begin with $ and _
  4. Names are case sensitive (y and Y are different variables)
  5. Reserved words (like JavaScript keywords) cannot be used as names

The Assignment Operator

In JavaScript, the equal sign (=)  is an “assignment” operator, not an “equal to” operator.

This is different from algebra. The following does not make sense in algebra:

x = x + 6

In JavaScript, however, it makes perfect sense: it assigns the value of x + 6 to x.

(It calculates the value of x + 6 and puts the result into x. The value of x is incremented by 6.)

JavaScript Data Types

JavaScript variables can hold numbers like 100 and text values like “John Doe”.

In programming, text values are called text strings.

Strings are written inside double or single quotes. Numbers are written without quotes.

If you put a number in quotes, it will be treated as a text string.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';

document.getElementById("demo").innerHTML =
pi + "<br>" + person + "<br>" + answer;
</script>

</body>
</html>

Output:

Creating JavaScript Variables

Creating a variable in JavaScript is called “declaring” a variable.

You declare a JavaScript variable with the var keyword:

var carName;

After the declaration, the variable has no value (technically it has the value of undefined).

To assign a value to the variable, use the equal sign:

var carName = "BMW";

Example:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var carName = "BMW";
document.getElementById("demo").innerHTML = carName;
</script>

</body>
</html>

Output:

One Statement, Many Variables

You can declare many variables in one statement.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var person = "John Doe", carName = "BMW", price = 300;
document.getElementById("demo").innerHTML = carName;
</script>

</body>
</html>

Output:

JavaScript Arithmetic

As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:

<!DOCTYPE html>
<html>
<body>

<p>The result of adding 6 + 4 + 2:</p>

<p id="demo"></p>

<script>
var x = 6 + 4 + 2;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output:

You can also add strings, but strings will be concatenated:

<!DOCTYPE html>
<html>
<body>

<p>The result of adding "John" + " " + "Doe":</p>

<p id="demo"></p>

<script>
var x = "John" + " " + "Doe";
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output:

Enjoy coding!

Leave a Reply

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