Refresher Time: Javascript
crash course on the fundamentals
Javascript is a programming language allows you to implement complex features on a webpage. Think of a car for instance. You have the make, parts and colors of the car (HTML & CSS) but what good is it if the car doesn’t have any cool functionality. Javascript is what makes the car rev loudly, accelerate to 100mph, drift, and everything else that makes it cool & dynamic.
Javascript is very complex and I probably won’t be able to cover every single topic out there in detail without writing a book but I will cover the basics I’ve learned so far because I sure do need a refresher. Also, keep in mind that Javascript != Java.
Variables: const, let, var
- const: it is constant i.e. this variable cannot changed once it’s defined. it cannot be reassigned but it can be mutated.
- let: you can reassign values.
- var: var will always be global whereas let is not. var has been used since the beginning of Javascript but for the most part this isn’t used anymore ever since const and let was introduced in ES6.
Data Types: String, Numbers, Boolean
String
single quotes, double quotes or backticks/template literals'I live my life a quarter-mile at a time'
"now you owe me a 10 second car"
`I said a ${5*2} second car not a ${100/10} minute car`
Numbers
let speed = 50;
let degree = 65.5; // no floats in JS, can be a decimal
let horsepower = -20.5;
Boolean
let fast = false;
let slow = true;
console.log(10 > 2) //true
console.log(10 < 2) //false
Loops: for, while
for loop
while loop
Operators ( + , - , * , ** , / , %)
Switch Statements
great for cases with many conditionals
Functions
This is the bread & butter of JS. There are few ways to write a function.
Function Declaration
function yourFunction(){
insert your code here
}yourFunction(); // this is how you call your function
Function Expression
A function is assigned to the variable explicitly. No matter how the the function expression is defined, it’s just a value stored in the variable addTotal. You can reassign addTotal to totalSum and it would still be the same outcome.
Arrow Functions
below is the same function but changed into an arrow function
Interaction: alert, prompt, confirm
Alert
Alerts simply show a message and waits for user’s confirmation.
Prompt
Prompts take in 2 arguments: title & default. The user can type something other than the default which will display once you click ‘ok’.
Confirm
Confirm shows a message and waits for the user to press “OK” or “Cancel”. It returns true for OK and false for Cancel.
Comparisons
- Greater & less than:
x > y
,y < x
. - Greater & less than or equals:
x >= y
,x <= y
. - Equals:
5 == 5.
SINGLE ‘=’ SIGN IS THE ASSIGNMENT OPERATOR. - Not equals:
a != b.
Bang operator is ‘opposite of’ so opposite of = is !=.
Code Structure: statements, semicolons, comments
statements: syntax & commands that perform actions
console.log('hello world')
alert('hello world')
semicolons: it may be omitted in most cases when a line break exists since JS does not strictly require it. Automatic Semicolon Insertion is a term when semicolons are added behind the scenes if it is needed.
comments: comments are used to temporarily ignore line(s) of code, make comments for reference, organization, etc.
// this comment won't run //
/* this too will be ignored by the engine */
Type Conversion: string, numeric, boolean
you may need to convert a number into a string, a boolean into a string or a value into true/false and this is where type conversion comes in handy.
String conversion: String() used when we need the string form of a value
let hot = true;
console.log(typeof hot); // booleanhot = String(hot); // now hot is a string "true"
console.log(typeof hot); // string
Numeric: Can be performed with Number(value). You cannot convert random text into a number for obvious reasons or else you’ll get NaN.
let randomNumber = "1234"; //string
randomNumber = Number(randomNumber);
console.log(typeof randomNumber) //number
Boolean: Boolean() will turn values into a TRUE or FALSE output.
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("hello world")); // true
console.log(Boolean("")); // false
console.log(Boolean(" ")); // true