Javascript Variables

In Javascript you have a component called a variable. This component is very similar to the variable in algebra. Variables in algebra are values that are identified by a letter. In Example (x=y+2), x and y are the variables. You can see that the values of x and y can change depending on which values you choose. If you set y to equal 4, then x=6.


In programming, a variable is simply a name or identifier of a location in computer memory. You may assign the variable a value or it may change over a period of time. When naming your variables, you may choose a character, a word, or a combination of characters, numbers, and the underscore (_). Your variable name must, however, begin with a letter or underscore. Javascript is case sensitive so the letters 'B' and 'b' are different. Therefore, you may have a variable named 'Amount' and a variable named 'amount' and your browser will not confuse the two variables. When naming variables, it is a good practice to give them meaningful names, which reflects the item it holds. So, a variable that holds the unit price of an item bought, might be called 'unit_price.'

Before you can use any variable in your program, you must first declare it. By declaring it, you are allocating space in the computer's memory so that only this one variable will have access to that space. In declareing variables, use the following format.

var Total = 0;




datatypes

Variables may hold numbers or strings. A string is a series of characters, numbers, and spaces. In the following examples we have code for declaring different datatypes.

datatype integer var total = 15; datatype floating point var price = 3.75; datatype string var name = "Unit_Price"; var sentence = "What is the cost of this item.";


arithmetic operators

Arithmetic operators take two numeric values called operands and results in one final number. Arithmetic operators are:

  • +      Addition
  • -       Subtraction
  • *       Multiplication
  • /       Division
  • ++    Increment - adds 1 to the number
  • --      Decrement - subtracts 1 from the number
  • -       Negation - negates the number
  • %     Modulus - the remaining integer value after a division operation

x = 5 + 3; value of x is 8 x = 6 - 2; value of x is 4 x = 4 * 4; value of x is 16 x = 20 / 5; value of x is 4
y = 6; z = ++y; value of z is 7, value of y is 7 because ++ appears before the variable, the operation of the increment occurs before the assignment to z.
y = 5; z = y++; value of z is 5, value of y is 6 because ++ appears after the variable, the operation of the increment occurs after the assignment to z.
y = 3; z = -y; value of z is -3
x = 8 % 3; value of x is 2. 8 divided by 3 leaves a remainder of 2.




comparison operators

Comparison operators take two operands and returns a true or false. Comparison operators are:

  • ==    Equal to - returns true if operands are equal
  • !=     Not equal to - returns true if operands are not equal
  • >      Greater than - returns true if left operand is greater than right operand
  • <      Less than - returns true if left operand is less than right operand
  • >=    Greater than or equal to - returns true if left is greater than or equal to right
  • <=    Less than or equal to - returns true if left is less than or equal to right


boolean operators

Boolean operators take two operators and returns a true or false. Boolean operators are:

  • &&   And
  • ||       Or
  • !       Not


string operator

Strings use the string operator + to concatenate strings together.

x = "Total," + "Cost"; value of x is now "Total, Cost"





Intro         Variables         Statements         If then else

Loops         Objects         Functions


Home   |   Business Directory   |   Festivals and Fairs (USA)