Number data type
Numbers in JavaScript are stored as 64-bit, floating point values. What this means, in English, is that numbers can range from 5e-324 (that’s -5 followed by 324 zeros) to 1.7976931348623157e+308 (move the decimal 308 spots to the right to see this giant number). Any number may have decimal points or not. Unlike most programming languages, JavaScript doesn’t have separate data types for integers (positive or negative numbers without a fractional part) and floating points (decimals).
Just how big is the biggest number JavaScript can use? Here it is, written out without scientific notation:
179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
When you declare a number variable, you compile it from all of the following elements:
- The var keyword
- The name you want to give your variable
- The assignment operator
- A number (or even an equation that resolves to a number
- A semicolon
Here are some examples of valid number variables declarations:
var numberOfDucks = 4;
var populationOfSpain = 47200000;
var howManyTacos = 8;
Number functions
JavaScript includes a built-in Number function for converting values to numbers. To use the Number function, simply put the value (or a variable holding the value) that you want to convert to a number between the parentheses after the Number function.
The
Number function produces four kinds of output:
- Numbers that are formatted as text strings are converted to numbers that can be used for calculations, like this:
Number("42") // returns the number 42
- Text strings that can’t be converted to numbers return the value NaN, like this:
Number("eggs") // returns NaN
- The Boolean value true returns the number 1, like this:
Number(true) // returns 1
- The Boolean value false returns the number 0, like this:
Number(false) // returns 0
parseInt() function
To JavaScript, all numbers are actually floating point numbers. However, you can use the parseInt() function to tell JavaScript to consider only the nonfractional part of the number (the integer), discarding everything after the decimal point.
parseInt(100.33); // returns 100
parseFloat(); function
You can use parseFloat() to specifically tell JavaScript to treat a number as a float. Or, you can even use it to convert a string to a number. For example:
parseFloat("10"); // returns 10
parseFloat(100.00); //returns 100.00
parseFloat("10"); //returns 10
String data type
Strings can be made up of any characters:
- Letter
- Number
- Punctuation (such as commas and periods)
- Special characters that can be written using a backslash followed by character
Some characters, such as quotes, have special meaning in JavaScript or require a special combination of characters, such as a tab or new line, to represent inside of a string.
Code
|
Outputs
|
\'
|
single quote
|
\"
|
double quote
|
\\
|
backslash
|
\n
|
new line
|
\r
|
carriage return
|
\t
|
tab
|
\b
|
backspace
|
\f
|
form feed
|
You create a string variable by enclosing it in single or double quotes, like this:
var myString = "Hi, I'm a string.";
It doesn’t actually matter whether you use single or double quotes, as long as the beginning and ending quotes surrounding the string match up.
If you surround your string with single quotes, you can actually use double quotes within that string without a problem. The same goes for if you surround your strings with double quotes; you can use single quotes within the string without a problem.
However, if you create a string and surround it with one type of quote, you can’t use that type of quote inside the string, or the JavaScript parser will think you mean to end the string and will generate an error.
Escaping quotes
The solution to the problem of not being able to include quotes inside of a string surrounded with that type of quotes is to preface the quotes with a \. Adding a backslash before a quote is called escaping the quotes.
String functions
JavaScript includes many helpful functions for working with and converting strings.
Here’s a list of the most frequently used built-in string functions:
- charAt( ) produces the character at a specified position. Note that the counting of characters starts with 0:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.charAt(3));
// returns a
- concat() combines one or more strings and returns the incorporated string:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.concat(‘ We love JavaScript!’));
// returns JavaScript is Fun! We love JavaScript!
- indexOf () searches and returns the position of the first occurrence of the searched character or substring within the string:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.indexOf(‘Fun’);
// returns 14
- split() splits strings into an array of substrings:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.split(‘F’));
// returns ["JavaScript is ", "un!"]
- substr() extracts a portion of a string beginning at "start" through a specified length:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.substr(2,5));
// returns vaScr
- substring() extracts the characters within a string between two specified positions:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.substring(2,5));
// returns Vas
- toLowerCase() produces the string with all of its characters converted to lowercase:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.toLowerCase());
// returns javascript is fun!
- toUpperCase() produces the string with all of its characters converted to uppercase:
var watzThisString = ‘JavaScript is Fun!’;
console.log (watzThisString.toUpperCase());
// returns JAVASCRIPT IS FUN!
Boolean data type
Boolean variables store one of two possible values: either true or false.
Boolean variables are often used for storing the results of comparisons. You can find out the Boolean value of a comparison or convert any value in JavaScript into a Boolean value by using the Boolean() function. For example:
var isItGreater = Boolean (3 > 20);
alert (isItGreater); // returns false
var areTheySame = Boolean ("tiger" === "Tiger");
alert (areTheySame); // returns false
The result of converting a value in JavaScript into a Boolean value using the Boolean() function depends on the value:
The result of converting a value in JavaScript into a Boolean value using the Boolean() function depends on the value:
- In JavaScript, the following values always evaluate to a Boolean false value:
- NaN
- undefined
- 0 (numeric value zero)
- -0
- "" (empty string)
- false
- Anything that is not one of the preceding values evaluates to a Boolean true. For example:
Boolean values are written without quotes around them, like this:
var myVar = true
On the other hand, var myVar = “true” creates a string variable.
NaN data type
NaN stands for Not a Number. It’s the result that you get when you try to do math with a string, or when a calculation fails or can’t be done. For example, it’s impossible to calculate the square root of a negative number. Trying to do so will result in NaN.
undefined data type
Even if you create a variable in JavaScript and don’t specifically give it a value, it still has a default value. This value is "undefined".