Values and Variables in JavaScript

Values and Variables in JavaScript

Like other programming languages JavaScript also has Values and Variables. JavaScript Value is piece of information while Variable is used to store that Value.

Values in JavaScript

As I said earlier Value is any piece of information or data that you might see or use in your code. Let's see a cool example of code.

<!DOCTYPE html>
<html>
<body>
<script>
 alert("Hello World!");
</script>
</body>
</html>

In above example, I have written an alert function (a predefined JavaScript function) in my script tag. An alert function simply displays a dialog box with the VALUE we pass to it. The output of alert function looks like this.

JavaScript Values

Yes you read it right, the text written inside the brackets of the alert function is actually the Value, that we have passed to this function.

The piece of text (collection of different characters) is commonly known as String in JavaScript. In JavaScript the String is wrapped inside single or double quotations marks.

Example of JavaScript Values

Coming back to Values, any type of data or information is a Value.

Let's say you want to use month's names somewhere in your code, the name of first month (January) is also a value. The random number, let's say 5213 is also a Value.

Why Values are used in JavaScript

Values are very important in any programming language, because we perform different operation on these values to get some results. So we have to reuse them again and again and that's where Variables come in handy.

In JavaScript we can perform a lot of operations (like mathematical operations) on Values to get the desired result.

Variables in JavaScript

A Variable is a name or a container of our Value. Instead of writing the values again and again we can simple assign that piece of information to our Variable and use it anywhere we want.

For the above example we can assign that string (Hello World!) to a variable and then we can use that variable in our alert function. The output would be exactly the same.

<!DOCTYPE html>
<html>
<body>
<script>
 var a="Hello World!";
 alert(a);
</script>
</body>
</html>

Why Variables are Used in JavaScript

JavaScript Variables are used to store JavaScript Values. In the above code, I have used a keyword var to declare or create a variable named "a" and then assigned a value (string type in this case) to our variable.

Then I have simply used that variable in the alert function and the output is still the same as shown in above image.

We can assign any string or value to our variable, let's look at another example.

<!DOCTYPE html>
<html>
<body>
<script>
 var myVariable="This is How To Code School";
 alert(myVariable);
</script>
</body>
</html>

In the above example I have created a variable named myVariable and assigned a string (This is How To Code School) to it. Then I have passed this variable to the alert function and it has worked just fine.

JavaScript Variables

Wherever I use this variable, it will always refer to the value that I assigned to it.

Naming Rules for JavaScript Variable

There are some rules that we need to keep in mind while naming our variables or creating a javascript variable.

  • Variable name can only start with a letter, underscore ( _ ) or a dollar ($) sign. It cannot start with a number.
  • Outside of first character of the name of your variable, your variable can contain numbers, letters, characters etc.
  • Spaces are not allowed while naming a variable.
  • Variable name can be as long as you want.
  • Variable name should be unique.

JavaScript variable can contain many types of data, string, number etc. The equal sign (=) used in assigning the value to variable is called assignment operator.