Data Types & Variables
(1) Intro to Data Types
Different data types:
- Numbers
- Strings
- Booleans
- Undefined
- Null
(2) Numbers
Arithmetic operations – Type into the console the arithmetic expressions just as you would in a calculator without the “=” sign. It will automatically populate the answer.
- 2 + 10 – 19 + 4 – 90 + 1 = -92
- -20 + -19 – (-10) – (-1) + 24 = -4
- (10/5) * 4 – 20 = -12
- 4096 % 12 = 4
Comparing numbers
- 5 > 10 Returns false
- 5 < 10 Returns true
- 5 == 10 Returns false
Operator | Meaning |
---|---|
< | Less than |
> | Greater than |
<= | Less than or Equal to |
>= | Greater than or Equal to |
== | Equal to |
!= | Not Equal to |
(3) Comments
Comments are embedded into your code because when your code gets more complicated you’ll forget what your lines of code actually does. There are two ways in Javascript to write comments into your code without the code interpreter to execute the code:
- // two forward slashes used for one line of comments
- /* forward slash star and star forward slash is used if you are writing a lot of comments */
(4) Quiz
(5) Strings
Strings are just a string of letters or numbers together within quotes. The quotes can either be single quotes or double quotes but they do have to match. If you forget to put the string in quotes, the interpreter will think you’re referring to a variable and not a string.
(6) String concatenation
You can add strings together to form complex string data. To concatenate strings, you use the + operator. i.e. “Steven” + ” Lien” would return “Steven Lien”
Implicit type coercion is a feature in JavaScript that will automatically convert number data into string data so that you’re adding together the same data type. i.e. “Hello” + 5*10 becomes Hello50. The 50 is no longer number data but string data. This phenomenon is called implicit type coercion.
(7) Variables
Variables are used to store data for future use instead of one time uses. Variables can be any type of data not just numbers. To create a variable, there are 4 different parts to it.
- var = keyword that creates a variable
- variableName = You assign an arbitrary name for the variable that you’ll be able to remember later
- “=” = the assignment operator
- value you want to assign to the variable
I.e. var name = “zoe”
(8) Quiz: Converting Temperatures
var celsius = 12;
var fahrenheit = celsius * 1.8 +32
console.log(fahrenheit);
(9) String Index
var name = "Steven";
name[0]; returns "S"
(10) Escaping strings
When you want to use quotes within a string you need to use the backslash to escape special characters so that the interpreter doesn’t misread your quotes.
"The man whispered, \"please speak to me.\""
Special characters that need to be escaped:
Code | Character |
---|---|
\\ | \ (backslash) |
\” | ” (double quote) |
\’ | ‘ (single quote) |
\n | newline |
\t | tab |
(11) Comparing Strings
You can use the “==” (equals) and != (not equals) to compare strings.
“Yes” == “yes” returns false because javascript compares the ASCII value of each letter and capital letters have a lower value on the ASCII chart.
(12) – (15) Quiz
Couldn’t solve 15
(16) Booleans
Booleans only has a True/False, Yes/No, On/False, 1/0.
(17) Quiz
(18) Null, Undefined, and NaN
Null = is the value of nothing
Undefined = a value exists but it has not been defined yet
NaN = Not-A-Number (Usually shown when you are doing a math calculation and the calculation doesn’t compute. I.e. dividing a string with a number “hello”/5 or calculating the square root of -10 Math.sqrt(-10)
(19) Equality
% is the modulus operator. It’s a math function that gives you the remainder after a number is divided by another number. I.e. 9 % 5 = 4 (5 goes into 9 and the remainder is 4)
Implicit type coercion – The JavaScript engine will automatically convert your data into the appropriate type when performing operators that require same data type.
Strict equality – If you’re performing an equality check and DO NOT want the JavaScript engine to perform implicit type coercion then you’ll add an additional “=” to either “==” or “!=”
(20) – (24) Quiz
(25) Lesson 2 Summary
Data types:
- Numbers
- Strings
- Booleans
- Null
- Undefined
Storing Data in Variables
Basic Operations
Pingback: Udacity – Javascript – Lesson 1 – MilliardCo.