WEB DEVELOPER SITE

# JavaScript Variables

## JavaScript variables

Like algebra, JavaScript variables can be used to hold values (such as x = 5) and expressions (such as z = x + y).

Variables can use short names (such as x and y) or more descriptive names (such as age, sum, totalvolume).

• Variables can also start with \$ and _ symbols (though we don't recommend this)
• Variable names are case sensitive (y and Y are different variables)
 Both JavaScript statements and JavaScript variables are case sensitive.

## Like algebra

x = 5
y = 6
z = x + y

In algebra, we use letters (such as x) to hold values (such as 5).

With the expression z = x + y above, we can calculate the value of z as 11.

In JavaScript, these letters are called variables. Think of variables as containers for storing data.

## Much Like Algebra

In this precedent, item1, item2, and complete, are variables:

### Example

var item1 = 8;
var item2 = 7;
var all out = itme1 + item2;
Try it Yourself »

In programming, much the same as in polynomial math, we use factors (like price1) to hold values.

In programming, much the same as in polynomial math, we use factors in articulations (complete = price1 + price2).

From the precedent above, you can ascertain the aggregate to be 11.

## JavaScript Identifiers

All JavaScript variables must be identified with unique names.

These remarkable names are called identifiers.

Identifiers can be short names (like x and y), or increasingly graphic names (age, entirety, totalVolume).

The general principles for building names for factors (remarkable identifiers) are:

• Names can contain letters, digits, underscores, and dollar signs.
• Names can likewise start with \$ and _ (however we won't utilize it in this tutorial)
• Names are case delicate (y and Y are distinctive variables)
• Reserved words (like JavaScript watchwords) can't be utilized as names

## The Assignment Operator

In JavaScript, the equivalent sign (=) is a "assignment" administrator, not an "equal to" operator.

This is not the same as variable based math. The accompanying does not bode well in algebra:

x = x + 8

In JavaScript, be that as it may, it bodes well: it relegates the estimation of x + 8 to x.

## JavaScript Data Types

JavaScript variables can also hold other data types, such as text values (name = "Bill Gates").

In JavaScript, a piece of text like "Bill Gates" is called a string.

There are many types of JavaScript variables, but for now, we are only concerned with numbers and strings.

When you assign a text value to a variable, you should enclose the value in double or single quotes.

Do not use quotes when the value you assign to a variable is numeric. If you surround the value with quotes, the value is treated as text.

### Example

var pi = 3.14;
var individual = "Sophie";
var answer = 'Yes I am!';
Try it Yourself »

## One statement, multiple variables

You can declare many variables in one statement. The statement starts with var and the variables are separated by commas:

var lastname = "Doe", age = 30, job = "carpenter";

Declarations can alsospan multiple lines:

var lastname = "Doe",
age = 30,
job = "carpenter";

Multiple declarations in a statement cannot be assigned the same value:

var x, y, z = 1;

x, y is undefined and z is 1.

## Value = undefined

In computer programs, variables with no value are often declared. A variable declared without a value is actually undefined.

After executing the following statement, the value of the variable carname will be undefined:

var carname;

## Redeclaring JavaScript variables

If you redeclare a JavaScript variable, its value will not be lost:

After the following two statements are executed, the value of the variable carname is still "Volvo":

var carname = "Volvo";
var carname;

## JavaScript arithmetic

You can do arithmetic with JavaScript variables, using operators such as = and +:

## Example

y = 5 ; x = y + 2 ;