A variable is basically a container or place holder for storing data values.
Variables are fundamental to all programming languages. Variables are used to store data, like a string of text, numbers, etc. The data or value stored in the variables can be set, updated, and retrieved whenever needed. In general, variables are symbolic names for values.
You can create/ declare a variable using the
var myName; var age; var job;
In this example,
job are variables.
So far we have created/declared variables.
But these are just names, they don’t have any data or value.
The Assignment Operator
=) is called an assignment operator. The assignment just means ‘set a value ‘ to a variable.
Let’s use the variables we’ve created above.
var myName = 'John'; var age = 18; var job = 'Waiter';
and text values like “John Doe”, ‘Jane Doe ‘.
In programming, text values are called strings.
Strings are written inside single or double-quotes. Numbers are written without quotes.
If you put a number inside a quote it will be treated as a string, not a number.
The difference is on numbers computers can do calculations on strings or text they can’t.
var pi = 3.14; var person = "Lia Sue"; var question = 'Who are you?';
- Names must begin with a letter.
- Names can contain letters, digits, underscores, and dollar signs.
- Names can also begin with $ and _
- Names are case sensitive (z and Z or x and X are different variables)
- A variable name cannot contain spaces.
- A variable name cannot start with a number.