JavaScript
Has 8 different data types
Undef, null, string, bigint, Boolean, number, object
Primitive types Reference Types
String Object
Num Array
Boolean Function
Undef
null
Primitives
Let name = ‘Helen’; //String literal
Let age = 30; //Number literal
Let isApproved = true; //Boolean literal
Let firstName = undefined; //uncommon to use undef. The type of this is also undef
Let lastName = null; // used to clear the value. The type of null is object
Let to declare keyword
let name = ‘Mosh’;
//Cannot be reserved keyword (let, if else var etc). You’re going to get an error if its reserved
//Meaningful var names plz
//Don’t start with a number
//No space or hyphen
Don’t use var! Use let
This is allowed but modern conventions like one name per var.
Let firstName = ‘Helen’, lastName= ‘Lin’;
Why don’t we use var anymore? Block vs function scope
Remember block is like a block of code, if you have a for loop like so, the i is only declared within that for loop, it doesn’t exist outside of it.
For Block scope, the Example class is the largest parent, and its children (processArray and for loop) can access its variables. Example class can’t access the children’s
variables
In ES5, scope is based on functions, so if you declare a variable inside a fxn, you can use it anywhere. So you could use that i outside too
You also have the global scope so each time you declare a var outside a fxn, belongs to the global scope
Why do we stop using the var keykword?
Scope reasons. Most programs have block scope. If you have function scope then two for loops end up using the same i and overwriting the first for loop. The function
doesn’t stop at the curly braces.
Unlike var, when you use let, a variable with the same name can only be declared once.
(Prevents you from making overwriting errors)
Const
Const is read only and won’t let you reassign it.
Ex. You want to keep GST 5% and don’t want people to accidentally change it.
Const GST = 5%
Const GST = 7% would result in error
However, const is still mutable
Const S = [5,6,7]
S = [1,2,3] error
But S[2] = 45;
Will result in S being [5,6,45];
How to ensure your data doesn’t change
JS provides a function called Object.freeze to prevent data mutation.
Obj.review and object.newProp will both result in errors
Dynamic Typing
JS is a dynamic language. What does that mean?
Static = once you declare a variable, the type of that variable never changes
Dynamic = type can change at runtime
Objects
Object oriented programming, an instance
Dot notation or bracket? Usually dot notation is cleaner, unless the variable is not determined until runtime (ex in a user selection)
//Bracket notation
Let selection = ‘name’;
Person[selection] = ‘Mary’;
Functions
A param is what we have at the time of declaration but the argument is the value that is actually supplied.
Uninitialized variables
They will be undefined. If you concat a string with an undefined, the whole thing will be undefined.
Immutable Strings
In JS, strings value are immutable; they can’t be altered once created.
You cannot do something like this
Let myStr = “Bob”;
myStr[0] = “J”;
Instead, you just assigned it with a whole new value, like this
myStr= “Job”;
Global Scope and functions
In JS, scope refers to visibility of variables.
Variables outside of a function block have Global scope. They can be seen everywhere in your JS code. Variables declared without const or let are automatically
created in the global scope which can be bad elsewhere. Always declare with let or const.
Variables declared within a function, as well as the function params, have local scope. That means they are only visible within that function.
If you have global and local variables with the same name, the local takes precedence over the global one (not advised to have same name variables however)
Understanding Undefined value returned from a function.
Ex. This fxn addFive doesn’t return anything, its returned value is undefined.
addFive(){
Sum += 5;
Strict Equality
Unlike the equality operator, which attempts to convert both values being compared to a common type, strict equality just compares it as it is. If they have different
types, they are unequal and === will return false.
Greater than and less than operator
These operators will also convert data types while comparing