Javascript variables, rules, examples

5/5 - (1 vote)

In other programming languages when you define a variable, you must specify its type, ie what type of information will be stored in the variable, like integer, boolean, array, etc;

JavaScript is different! In JavaScript we create a generic variable and then put whatever type of value we want in it.

To create a variable in JavaScript You must use the word var, all written in lowercase and then the name you want.

All JavaScript variables must be identified with unique names.

The name that you use for your JavaScript variable, must respect next rules:

  • must begin with a letter, can’t start with a number;
  • it must be one word, can contain letters, digits, underscores, and dollar signs. Spaces are not allowed!
  • are case sensitive, so for example a and A are different variables;
  • reserved JavaScript keywords (like alert) cannot be used as names;
var x;
var y;
var myName;

The above lines we created variables!
The variables exist now, but they do not contain any value.

A variable declared without a value will have the value undefined.
They are considered as undefined, which has a special meaning in JavaScript.

To assign a value to the variable, use the assignment operator (the equal sign):

The Assignment Operator

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

x = 7;
y = 5;
myName = "John";

You can assign a value to the variable when you declare it, like:

var x = 7;
var y = 5;
var myName = "John";

More then that, the word var is not even required.
You can create and assign value to the variable, like:

myName = "Mihai";

JavaScript will go looking for an existing variable called myName, but if it does not find it, it will just make it.
!!! Is recommended to use var when defining javaScript variables.

You can declare many variables in one statement.
To do this separate the names of the variables with commas, like:

var x, y, myName;

You can declare and assign values to many variables in one statement.

var x = 7, y = 5, myName = "John";

The declaration can span multiple lines, like:

var x = 7,
y = 5,
myName = "John";


Re-Declaring JavaScript variables

If you re-declare a JavaScript variable, it will not lose its value.

<p id="demo"></p>
var color = "red";
var color;
document.getElementById("demo").innerHTML = color;

//result: red

JavaScript comments

JavaScript comments can be used to explain JavaScript code.
We can use Single Line Comments or Multi-line Comments

Single Line Comments

Single line comments start with //.

// Declaring name
var myName = "John";

Multi-line comments

Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.

Declaring name
That I will use in form
var myName = "John";

Single quotes and Double quotes

In JavaScript, you can use either double quotes or single quotes to assign a text to a variable.

The only rule that must follow is to not mix.
Do not open the text with double quotes and close it with single quotes.

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment