Hexadecimal numbers
Hexadecimal colour values
This example demonstrates how Javascript can convert between hexadecimal and decimal numbers by prefixing a (hexadecimal) number with 0x. When a hexadecimal number is parsed, it outputs is decimal equivalent.
This little trick avoids having to write a function to do the conversion.
Number schemes
The decimal system
The numbering scheme in everyday use is the decimal (or denary) system and numbers are represented by 10 digits i.e. the numbers 0 to 9.
Counting starts at zero, (see counting wheels below). Rotating the wheel anticlockwise adds 1 to the current count until the number reaches 9 whereupon the count is reset to 0.
Radians and degrees
Radians and degrees (or angular measure)
When measuring angles we usually
measure them in degrees. There are 360 degrees in a circle.
I.e. a degree simply divides a circle into 360 segments.
An alternative radial measure is known as a radian. A radian is the
angle formed by measuring the radius around the
circumference of a circle as show by the diagram. 1
radian is approximately 57.32 degrees.
Operators
Operators
The most common operators are mathematical operators; +, -, /, * (add, subtract, divide, multiply) for example. Operators can be split into two groups, comparison operators and assignment or 'action' operators. Comparison operators test to see if two variables relate to each other in the specified way, for example, one variable is a higher number than the other. Other operators perform an action on a variable, such as increasing it by one.
Numbers
Math constants
JavaScript understands numbers in several formats, allowing you to specify numbers in hex, decimal, and octal. If a 0 precedes a number and there is no number higher than 7, the number is considered to be in octal (base 8) and if the number is preceded by 0x, then it is considered to be in hex (base 16) and can also contain characters A, B, C, D, E, F. Neither may contain decimal points.
With decimal numbers, 12e+4 may be used to replace 12x104 and 12e-4 may be used to replace 12x10-4 etc.
String object
String Object
The String object is used to manipulate a stored piece of text. String objects are created with new String().
Strings
Strings
A string literal is 0 or more characters enclosed in either double, or single quotes. For example
"Hello World"
'Sun wind and rain'
"123"
Strings are a fundamental to any programming language and as well as all the comparison operators strings may be concatenated (joined) together using the concatenation operator, the plus symbol ( + ). for example
Data types
Javascript data types
Javascript is a loosely typed language, meaning you do not have to declare the data types of variables explicitly. Javascript uses a variant datatype and performs conversions automatically as necessary.
JavaScript has six types of data, usually referred to as primitive data types. The main types are strings, numbers, objects, and Booleans. The other two are null and undefined .
Ternary if
Ternary if statement
This provides a shorthand way to assign different values to a variable, depending on a condition.
var myVariable = document.getElementById("color").value
myVariable == "red" ? 1 : 0;
This is identical to:
Switch ... case
Javascript switch .. case statement
The following function performs the same test using the switch case statement. As you can see it is much more compact and consequently easier to read.

