Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript: Understanding the Weird Parts in 35ish minutes - Part 2/6
Credits
Prerequisite

Section 3 - Types and Operators

Lesson 19 - Conceptual Aside (Types and Javascript)

#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;
Lesson 20 - Primitive Types

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)
Lesson 21- Conceptual Aside (Operators)

#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.

Lesson 22- Operators Precedence and Associativity

#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

Lesson 24- Conceptual Aside (Coercion)

#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
Lesson 25- Comparison Operators

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
Lesson 27- Existence and Booleans
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.

Lesson 28- Default Values
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();
Lesson 29- Framework Aside (Default Values)
Lib_1.js
var libraryName = “Lib 1”;
Lib_2.js
window.libraryName = window.libraryName || “Lib 2”; 

Subscribe

If you want a gist of the actual document, subscribe to the newsletter.


Bookmark

Unlike life, keyboards do have shortcuts, press COMMAND+D to make this an easily accessible resource by bookmarking it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.