Question | Click to View Answer |

Perform these exercises in the JavaScript console of your favorite web browser. What does the following expression return? ```
3 + 2;
``` |
```
5
``` 3 and 2 are called operands and + is the operator. |

What does the following expression return? ```
typeof(3);
``` |
```
"number"
``` There are a handful of data types in JavaScript (number, string, boolean, object, and undefined). Notice that there is only a single data type for numbers, not separate data types for integers and floating point numbers like some other languages. |

What does the following expression return? ```
typeof(3) === typeof(4.32);
``` |
```
true
``` 3 is a "number" and 4.32 is a "number" as well, so the equality comparison of the types returns true. JavaScript has the same data type for integer and floating point numbers. |

What does the following expression return? ```
5 / 0;
``` |
```
Infinity
``` Infinity is a very large number in JavaScript that is bigger than the biggest number JavaScript can represent. Technically, division by zero should return undefined, but this is a JavaScript quirk. |

What does the following expression return? ```
3 / "bob";
``` |
```
NaN
``` NaN stands for "not a number" and is returned when a mathematical operation cannot yield a numeric result. |

What does the following expression return? ```
NaN === NaN;
``` |
```
false
``` NaN is not equal to any other value, including itself. |

What does the following expression return? ```
typeof(NaN);
``` |
```
"number"
``` NaN has a "number" type. |

What does the following expression return? ```
isNaN(NaN);
``` |
```
true
``` The isNaN method returns false if the argument is a number and true otherwise. isNaN returns true if the argument is NaN. |

What does the following expression return? ```
Math.pow(2, 3);
``` |
```
8
``` This expression represents 2 raised to the power of 3. The Math object contains methods to assist with common mathematical operations. |

Describe how the following expression assigns a variable to a value. ```
var first_name = "cindy";
``` |
The var keyword is used to declare a variable. In this case the var keyword declares the variable first_name. Additionally, this expression assigns the variable to the value "cindy". |

What is the value of the hello variable in the following expression? ```
var hello;
``` |
```
undefined
``` When a keyword is declared, but not assigned to any value, its value is undefined. |

What does the following expression return? ```
var y;
y === "cool";
``` |
```
false
``` The variable y has been declared, but it has not been assigned to anything, so its value is undefined. When undefined is compared with the string "cool" with the === equality operator, the return value is false. |

What does the following expression return? ```
"some" + " person";
``` |
```
"some person"
``` "some" and "person" are the operands and + is the operator. When the operands are strings, the + operator performs string concatenation. |

What does the following expression return? ```
> var first = "Bart";
> var last = "Simpson";
> first + " " + last
``` |
```
"Bart Simpson"
``` In this example, three strings are concatenated. |

What does the following expression return? ```
"cool".length;
``` |
```
4
``` The length property of strings returns the number of characters in a string. |

What does the following expression return? Explain the answer. ```
"phat" === "phat";
``` |
```
true
``` The === operator considers two strings equal if they both have the same length and the same characters in the same order. |

What does the following expression return? ```
typeof("cats");
``` |
```
"string"
``` "string" is one of the fundamental types in the JavaScript language. The other fundamental types are numbers, booleans, objects, and undefined. |

What does the following expression return? ```
3 + "bob"
``` |
```
"3bob"
``` JavaScript cannot add a number and a string without converting the number to a string first. JavaScript converts 3 to "3" and then concatenates the two strings. 3 can be converted to "3" with this code: String(3); |

Round the number 4.87 to the nearest integer. |
```
Math.round(4.87);
``` |

Divide the number 6 by the string "2" and explain the result. |
```
6 / "2" // returns 3
``` JavaScript cannot divide a number by a string, so it first converts the string to a number, and then performs the division. The string "2" can be converted to a number with this code Number("2"); Many programming languages raise exceptions for type mismatches, but JavaScript aggressively converts types instead. |

What does the following expression return? Explain the result. ```
3 * "bob";
``` |
```
NaN
``` JavaScript attempts to convert "bob" to a number so multiplication can be performed, but the string "bob" cannot be converted to a number. Number("bob") returns NaN. 3 multiplied by NaN also returns NaN. |

Declare the variables x and y. |
```
var x, y;
// OR
var x;
var y;
``` |

Set the variable hobby to the string "programming". |
```
var hobby = "programming";
``` |

What does the following expression return? ```
var sport;
sport === undefined
``` |
```
true
``` A variable that has been declared, but has not been assigned to any value equals undefined. |

Demonstrate that "brogrammer" has the type "string". |
```
typeof("brogrammer");
``` |