Beginner JavaScript – Chapter 4 – JavaScript Data Types

Beginner JavaScript – Chapter 4 – JavaScript Data Types

Note:

As stated in my initial post, this is part of a series of JavaScript-related posts. As I am becoming knowledgeable in JavaScript, I am posting out what I’ve learned, to re-enforce my knowledge, as well as maybe helpful other web designers/developers in learning JavaScript. If I have mis-stated anything, please feel free to post it in the comments. You’ll help me, and others in learning JavaScript. Thanks!

There are three types of data in JavaScript:

  • Number
  • String
  • Boolean

Number Data Type

The number data type can be used to represent either integers (whole number, i.e. 24, 48, 12876) or decimals (one or more decimal digits, i.e. 24.78, 1785.23, 12.2358). Both of these values can contained a preceding plus or minus sign (to represent positive or negative numbers).

The number value can also include an exponent (i.e. 25478E.124). With this scientific notation, the exponent indicates the number of zeros to the left or right of the decimal. These are also referred to as floating-point numbers. Most applications will not used this type of number type.

Number types can hold very small & large numbers. You would normally run into this via some sort of arithmetic. If you do happen to go beyond the max, you’ll receive Infinity/-Infinity as a value.

Examples of Number data types:

25  // an integer
-48  // a negative integer
48.5  // a floating-point value
-225.82  // a negative floating-point value
-2.5e-9  // a floating-point value equal to -0.0000000025

String Data Type

String data type is stored within single or double quotation marks (quotes). Anything, including numbers, within quote marks are considered string data.

If a line break is required within a string, an escape sequence is used (\n)

Examples of Number data types:

"Dennis Deacon" // a string with double quotes
'The new date is '  // a string with single quotes
''  // an empty string
'This is a \n
line break'  // a string with a line break/escape  sequence

Boolean Data Type

Boolean data types are limited to two values; true or false, with no quotation marks. This data type would be used anytime one of two states is needed (i.e. yes/no, on/off, initialized/not initialized, etc.)

Examples of Number data types:

var status = true;  // applies the value of true to the variable status
var status = false;  // applies the value of false to the variable status