- Anthony Alicea, follow him on Twitter
- Javascript Basics
- JavaScript: Understanding the Weird Parts in 35ish minutes - Introduction
- JavaScript: Understanding the Weird Parts in 35ish minutes - Part 1/6
- Willingness to learn
- 35 min
#BIGWORD Dynamic Typing - You don’t tell the engine what type of data a variable holds, it figures it out while your code is running. Variables can hold different types of values because it’s all figured out during execution.
Other languages have Static Typing
bool isNew = ‘hello’; // an error in js
Javascript is Dynamically Typed
var isNew = true // no errors
isNew = “yup!”;
isNew = 1;
There are 6 primitive types in javascript. A primitive type is a type of data that represents a single value, ie not an object
- undefined: It represents lack of existence (you shouldn’t set a variable to this)
- null: It represents lack of existence (you can set a variable to this)
- boolean: true or false
- number: Floating point number (there’s always some decimals). Unlike other programming languages, there’s only one number type ….and it can make math weird.
- string: a sequence of characters both single and double quotes can be used.
- symbol: used in ES6 (the next version of javascript)
#BIGWORD Operators - A special function that is syntactically (written) differently, Generally operators take 2 params and return a result.
Example 1
var a = 1>2; //infix notation
console.log(a); //false
In the above example + sign is the operator it’s the addition operator and its actually the function.
#BIGWORD Operator precedence - Which function gets called first. Functions are called in order of precedence (Higher precedence wins)
#BIGWORD Associativity - What order operator functions get called in: Left to right or right to left (When functions have the same precedence)
Example 1
var a = 3 + 4 * 5;
console.log(a); //23
Example 2
var a=2 , b =3 , c=4;
a = b = c ;
console.log(a); //4
console.log(b); //4
console.log(c); //4
They all are equal because of associativity.
Example 3
var a = (3 + 4) * 5;
console.log(a); //35
Brackets have higher precedence
#BIGWORD Coercion - Converting a value from one type to another. This happens quite often in javascript because it’s dynamically typed.
Example 1
var a = 1+2;
console.log(a); //3
Example 2
var a = “Hello ”+ “World”;
console.log(a); //Hello World
Example 3
var a = 1 + “2”;
console.log(a); //12 Coerced 1 from number to string
Example 1
console.log(1<2<3); //true
Less than the operator has left to right associativity so 1 < 2 return true then true will be coerced to 1 and 1 < 3 will return true.
Example
console.log(3<2<1); //true
Less than the operator has left to right associativity so 3 < 2
returns false than false will be coerced to 0
and 0 < 1
will return true
Check
Number(undefined); // NaN
Number(null); //0
Check
Boolean(undefined); // false
Boolean(null); //false
Boolean(“”); //false
Boolean(0); //false
All of these things imply the lack of existence they convert to false.
Example 1
var a ;
if (a) {
console.log(‘Something is there’);
}
In the above example a will be converted to boolean. We can use coercion to check if the variable has some value.
Check
undefined || “hello” //hello
null || “hello” //hello
“” || “hello” //hello
**Example 1 **
function greet(name) {
name = name || “tony”; //OR operator behaviour
console.log(“hello “+name);
}
greet();
Lib_1.js
var libraryName = “Lib 1”;
Lib_2.js
window.libraryName = window.libraryName || “Lib 2”;
If you want a gist of the actual document, subscribe to the newsletter.
Unlike life, keyboards do have shortcuts, press COMMAND+D
to make this an easily accessible resource by bookmarking it.