To Documents

Javascript Literals, Variables, and Operators

 

Literals

We consider four types of literals: integer, floats, strings, and boolean.

  1. Integer   Positive, negative, or zero numbers that do not have a fractional part, such as 34, 643938, -21, -38472874, or 0.

    Javascript can represent any integer between -253 (-9007119254740992) and 253 (9007119254740992). Smaller or larger integers can be used, but some precision will be lost.

  2. Floating Point   Floating point literals are numbers with a fractional part, such as 46.1982, 0.00441, -237.1273. The literal 23.000 also qualifies as a floating point number, even though it could be converted to an integer without loss of precision. Floating point numbers can also be written in base 10 scientific notation. Here are some examples:

    Javascript Literal Scientific Notation Meaning
    3e2 3 x 102 300
    5.98e24 5.98 x 1024 Mass of Earth in Kilos
    9.11e-31 9.11 x 10-31 Mass of Electron in Kilos

  3. String   A sequence of characters delimited by single or double quotes.
    Examples of string literals: 'abc', "g32", '*&^(%$#', " " (space), '' (null string with no characters).

  4. Boolean   A boolean (or logical) literal can take on either of the values true or false.

  5. Special Values

 

Variables

A variable is a location in a computer that contains data, which might be changed during the execution of a Javascript script. There are three aspects to a variable:

  1. Name   The name of a Javascript variable must start with a letter (upper or lower case) and consist entirely of letters or digits. There is no limit to the length of a variable name, but names longer than about 12 characters become unwieldy. Here are some examples of variable names:

    Javascript is case sensitive, which means that capitalization matters. The names numberOfCustomers and numberofcustomers represent two different variables.

  2. Address   The address of a variable is the location in the computer's memory where the data is stored. The Javascript interpreter keeps a symbol table that maintains the correspondence between the variable's name and its address, so the programmer need not be explicity concerned about the address.

  3. Value   The value of a variable is the data currently stored at its address. A variable can contain an integer, floating point number, or a string.

Variable Declaration   Variable declaration in Javascript is strongly recommended, although many JavaScript programs will run even if variables are not declared. For example, to declare the variables x and y:

 

The Assignment Operator

The assignment operator is used to assign a value to a variable. For example,
x = 5;
assigns the value 5 to the variable x.

 

Variable Initialization

A variable can be declared and assigned a value in one line. This is called initialization. For example:

 

Operators

Here is a table of some common Javascript operators.

Symbol Operand 1 Operand 2 Meaning Precedence
- Number   Negation 1
* Number Number Multiplication 2
/ Number Number Floating Point Division 2
+ Number Number Addition 3
+ String String Concatenation 3
+ String Number Concatenation 3
+ Number String Concatenation 3
- Number Number Subtraction 3
< Number Number Less Than 4
<= Number Number Less Than Or Equal 4
> Number Number Greater Than 4
>= Number Number Greater Than Or Equal 4
== Number Number Equal To 4
!= Number Number Not Equal To 4
& Number Number Logical And 5
| Number Number Logical Or 6
= Variable Expression Assignment 7

Note: There is no JavaScript operator for exponentiation. Use the function Math.pow for exponentiation.