DataTypes

  • Numbers - are values that can be processed and calculated. You don't enclose them in quotation marks. The numbers can be either positive or negative.
  • Strings - are a series of letters and numbers enclosed in quotation marks. JavaScript uses the string literally; it doesn't process it. You'll use strings for text you want displayed or values you want passed along.
  • Boolean (true/false) - lets you evaluate whether a condition meets or does not meet specified criteria.
  • Null - is an empty value. null is not the same as 0 -- 0 is a real, calculable number, whereas null is the absence of any value.

Data Types

TYPE EXAMPLE
Numbers Any number, such as 17, 21, or 54e7
Strings "Greetings!" or "Fun"
Boolean Either true or false
Null A special keyword for exactly that – the null value (that is, nothing)

Integers

In JavaScript, you can express integers in 3 different Bases:

  • base 10,
  • base 8 (octal), and
  • base 16 (hexadecimal).

Base 8 numbers can have digits only up to 7, so a decimal value of 18 would be an octal value of 22.

Similarly, hexadecimal allows digits up to F, where A is equivalent to decimal 10 and F is 15. So, a decimal value of 18 would be 12 in hexadecimal notation.

In order to distinguish between these three bases, JavaScript uses the following notation.

Specifying bases in JavaScript

NUMBER SYSTEM NOTATION
Decimal (base 10) A normal integer without a leading 0 (zero) (ie, 752)
Octal (base 8) An integer with a leading 0 (zero) (ie, 056)
Hexadecimal (base 16) An integer with a leading 0x or 0X (ie, 0x5F or 0XC72)

Floating Point Values

Floating point values can include a fractional component. A floating-point literal includes a decimal integer plus either a decimal point and a fraction expressed as another decimal number or an expression indicator and a type suffix

  • 7.2945
  • -34.2
  • 2e3 means 2 x 103 => 2000
  • 2E-3 means 2 x 10-3 => .002

Floating point literals must, at a minimum, include a decimal integer and either the decimal point or the exponent indicator ("e" or "E"). As with integers, floating point values can be positive or negative.

Strings

Technically, a string literal contains zero or more characters enclosed, as you know, in single or double quotes:

  • "Hello!"
  • ‘245’
  • ""  // This example is called the empty string.

NOTE: the empty string is distinct from the null value in JavaScript.

NOTE: Strings are different from other data types in JavaScript. Strings are actually Objects. This will be covered later on.

Boolean

A Boolean value is either true or false.

Note: Unlike Java, C and other languages, in JavaScript Boolean values can only be represented with true and false. Values of 1 and 0 are not considered Boolean values in JavaScript.

Null Value

The null value is a special value in JavaScript. The null value represents just that – Nothing. If you try to reference a variable that isn’t defined and therefore has no value, the value returned is the null value. Likewise, with the prompt() dialog box, if the user selects the Cancel button, a null is returned. (example)

NOTE: This is distinct from a value of zero or an empty string where this is an "actual" value. The null value is indicated in JavaScript by the term null.

NaN (Not a Number)

In addition to these values, some functions return a special value called NaN – which means that the value is not a number, parseInt() and parseFloat() are an examples of functions that return NaN when the argument passed to them cannot be evaluated to a number.

NOTE: Values can be tested to see if they are NaN by using the isNaN() function which returns true or false based on the nature of the argument passed to the function.

Creating Values

In order to make working with data types useful, you need ways to store values for later use. This is where variables come in.