JavaScript Basics and Conditionals Review

Source Material

See the first two chapters of the second edition of Eloquent JavaScript for more detailed information:

JavaScript Basics…

Data Types and Values

What’s a value and what’s a data type?

Data Types

What are some data types that we know?

Data Types

A value’s data type determines how that value behaves when used in functions or transformed/combined with operators.

Numbers

Numbers can be positive or negative rational numbers, like 23, -42 and 3.14. However, there are also some special values that are numbers. What are they, and how do you get them?

Strings

What’s a string, and give an example of a string

A string is an ordered sequence of characters. You can tell that a value is a string if it is surrounded by single or double quotes:

"Surrounded by quotes!"

What’s an empty string?

""

Escape Characters

How do we write a backslash, quote, tab or a new line in a string?

"\\"
"\""
"\t" 
"\n"

Operators

What’s an operator and what are some examples of operators?

Numeric Operators

Name 5 binary arithmetic operators (they take two operands, one on either side), and their precedence

String Operators

We learned one operator that works on strings. What was it?

string concatenation, or +, is an operator that takes two strings and joins them:

"hello " + "there"

Boolean Values

Besides numbers and strings, there is another type that we talked about briefly. They are booleans. What are the possible values that a boolean can be?

true
false

Boolean Operators

What are some operators that return boolean values?

Boolean Expressions

You can create boolean expressions with these operators. What is the value that results from the following expressions? What type are they?

1 === "1"
12 !== "cat"
// boolean
false
true

Additional Comparison Operators

What are some other comparison operators that JavaScript has?

Additional Comparison Operators Continued

// false!
5 > 7

// true!
5 >= 5

Boolean Expressions Give Back Boolean Values!

Conditionals

A conditional is a way of allowing us to conditionally execute code based on a boolean expression.

If Statement

Conditionally execute a branch of code.

if (some_boolean_expression) {
	// do stuff here if expression is true
}

If / Else Statement

Execute one branch if condition is true, another branch if condition is false. An else must have a preceding if!

if (some_boolean_expression) {
	// do stuff here if expression is true
} else {
	// do stuff here if expression is false
}

If / Else If

Chain multiple conditions. You can add else at the end as well. Conditions are evaluated until the first true condition, at which point the if statement finishes immediately.

if (boolean_expression_1) {
	// do stuff here if expression 1 is true
} else if (boolean_expression_2) {
	// do stuff here if expression 2 is true
} else if (boolean_expression_3) {
	// do stuff here if expression 3 is true
}

Usage

Boolean Expressions

Note that within the parentheses is some expression that produces a boolean values (true or false).

if (boolean_expression_1) {
	// do stuff here if expression 1 is true
} 

Blocks of Code

Curly braces denote statements of code that are grouped together. Everything within the curly braces below is considered part of the if statement.

if (boolean_expression_1) {
	// do stuff here if expression 1 is true
} 

Logical Operators

Logical operators allow you to combine boolean values!

Using Logical Operators

Examples

// variable num is between 1 and 10
if (num >= 1 && num <=10) {
	console.log("it's between 1 and 10")
}

// answer is yeah or yes
if (answer === 'yes' || answer === 'yeah' ) {
	console.log("YUP!")
}

Converting from Strings to Numbers

Use the built-in function parseInt to convert a string to a number.

var num = parseInt(answer, 10);