Getting into Javascript
Example section for showcasing API endpoints
Getting Into JS
Introduction
https://raw.githubusercontent.com/coodict/javascript-in-one-pic/master/js%20in%20one%20pic.png
Primer
- Values
- Operations
- Variables
- Expressions and Statements
- Decisions
- Loops
- Functions
Values
- The difference between primitive values and non-primitive values is that one is actual just value and another is collection of values.
- You often come across
null
andundefined
more often. Both them are mean to define emptiness in a programming language. The fact that JS has both of those is due to it’s history. - The main difference between an
Array
andObject
is that: In Arrays, developer doesn’t get to decide the position but while in Object, developer has the ability name the position.
Programming Primer
Operations
-
If a operator operates on two operands. It’s Binary Operator
3+4
. Likewise, if a operator operates on one operand, it’s Unary Operator!true
-
It is called overloaded when same operator behaves in more than one way. For example,
+
is a operator, that can operate on operands which are both numbers and both strings. -
Operators
; is also a operator
Builtin fundamental Objects that we want to look at.
Use new
:
Object()
Array()
Function()
Date()
RegExp()
Error()
Don’t use new
:
String()
Number()
Boolean()
Converting Types
When working with + operator:
Number + Number = Number
String + Number = String
Number + String = String
String + String = String
Falsy & Truthy
Which values if try to convert them into a boolean would become true/false ?
Example
Boolean("")
would give false
Boolean(0)
would give false
Where, Implicit coercion can sometimes be useful,
“
If a feature is sometimes useful and sometimes dangerous and if there is a better option then always use the better option”— JS “The Good Parts” Crockford
Useful: when the reader is focused on what’s important
Dangerous: when the reader can’t tell what will happen
Better: when the reader understands the code
Checking Equality == vs ===
== checks value (loose)
=== checks value and type (strict)
🍏**==** allows coercion (types different)
🍏**===** disallows coercion (types same)
📍When the types are already the same, == or === behave the same. No corner cases, whatsoever.
Let’s see another example,
❗The above code can be bit harder to understand. So I am trying to take notes for my future self.
- If both the workshops, 1 & 2 objects have
topic
property assigned with valuenull
then if condition evaluates toTrue
and executes the code in between. - From what I have learnt so far
===
disallows coercion. So you are required to perform check whether each workshop1.topic / workshop2.topic is either null or undefined==
allows coercion. So javascript itself will try to coerce —> workshop1.topic values null and workshop2.topic values undefined. Now, that coercion is done,workshop1.topic == null
evaluates to true orworkshop2.topic == null
evaluates to false
- Finally, The second if conditional turns out to be more efficient and readable. This proves the point that when values involved are going to be different types (number, string, object, boolean,symbol,undefined ) then you will need to check equality wise.
👉==
is not about comparisons with unknown types
👉==
is about comparisons with known type(s). optionally where conversions are helpful.
StackOverflow
Which equals operator (== vs ===) should be used in JavaScript comparisons?
Scope
Where to let JS look for things? Things like variables in the memory. Is it local execution context? Or is it in global execution context?
- Nested Scope
- Closure
undefined vs. undeclared
These are two concepts around emptiness. undefined and undeclared looks like they are more or less the same things. But they a significantly different.
undefined
is probably a variable that is declared somewhere in the code but is not has been assigned with any value.undeclared
is not even declared. JS will try to look for the value under scope rules during the run time.
Function expressions
Themselves being assigned as values. They can be passed around.
While I already know, the difference between Anonymous and non-Anonymous functions are Kyle prefers to suggest to use named or non-anonymous functions as much as possible.
For example,
IIFE - Immediately Invoked Function Expressions
Here are super interesting findings
- In way 1, when teacher variable is declared again, it is created under a newer scope and when you log teacher, the console prints Suzy
- Yet if way 2 is not there, and way 1 only exists, it will not effect
teacher
in global scope at all. Console will still log"Kyle"
- In way 2 if its’ there, then
teacher
will be overwritten from"Kyle "
to"Saif "
Now the above 3 steps are not needed as ES6 now supports block scoping. Simply use let
Preferably, use "var "
at function level and "let "
at block level.
Another Best practice,
If you look at the above code, there is a simply a block inside which prefix and rest are used. It is better to limit them to that particular block scope because, they are not needed at function level anyway.
Closure
Closure is when a function “remembers” the variables outside of it, even if you pass that function else where.
this & Prototypes
- this
- Prototypes
- class
this
You may have seen the this keyword at many places. Maybe within functions.
A function’s this references the execution context for that call, determined entirely by how the function was called. So a this - aware function can thus have a different context each time it’s called, which makes is so much flexible and reusable.
So here, How the function was called broadly sums up into 4 ways,
- Implicit Binding
- Explicit Binding
- Yet to know (Not covered in the course) — Looks like new keyword
- Yet to know (Not covered in the course)
Implicit Binding
The above code example very clearly demonstrates this. The ask()
method here is a this-aware function. Which means function definition has this
in it.
workshop
is a object.workshop.ask()
is a statement. This statement is how theask()
function is called.- workshop object .(dot) ask will implicitly bind the this in
ask()
function definition to contain the context of workshop object
Explicit Binding
- In the above case, Observe that: how the this aware function is called is different.
- function
ask()
is present in the global scope.otherClass()
has a statement that invokes the ask method. ask.call(myContext,"why?");
explicitly binds the context object to ask() method. So this will contain the context ofmyContext
object.
Prototypes
This is very clear and interesting example.
- You create a
Workshop
function in global scope. It isthis
aware function. - To this
Workshop
’s prototype method, developer adds a methodask()
. Notice thatask()
method references tothis.teacher
which is part ofWorkshop
function. - deepJS and reactJS are just two variable which are assigned with respective Instances.
- Now when for example,
deepJS.ask()
is invoked. Notice ask() is not part of Workshop function’s instance but it is part of Workshop’s prototype object’s instance. Whenask()
executes, it is called with deepJS context(which is instantiated by “Kyle”). - Thus it prints,
// Kyle Is prototype a class?
Class keyword
Practice
Wrapping Up
Only way to learn JavaScript is by Writing it.