Lightning Talk 3 - How To Read Javascript
-David Wong
Slides
Recording
Overview
- Expression and Statements
- Data Types
- Functions
Expressions
- Code that evaluates to a value
- Can be assigned or used as operands
3 + 4
const value = 3 + 4
Statements
- Carry out actions such as creating a variable, functions, looping or evaluating code based on a specific condition
- Can only be declared
function testNum(a) {
let result;
if (a > 0) {
result = "positive";
} else {
result = "NOT positive";
}
return result;
}
console.log(testNum(-5));
Declarations
- Binding identifiers to values
- The terms Statements and Declarations have precise meaning in the formal syntax of Javascript. For example, If statement require statements in its body rather than declarations
const varName = "";
let varName2;
function foo() {}
if (true)
const test = 123;
Primitive Types
- Data that is not an object
- Has no methods or properties
- Immutable: Meaning it can't be altered. Assigning a value is not altering. Altering is directly modifying the data in memory.
- When a property is accessed on a primitive value, Javascript automatically wraps the value into the corresponding wrapper object and accesses the property on the object instead.
- Examples;
- string
- number
- boolean
- etc
const thisIsANumber = 123;
thisIsANumber.<methods()>
const numberIntoString = thisIsANumber.toString();
typeof thisIsANumber
typeof numberIntoString
numberIntoString.<methods()>
Objects
- Everything in Javscript is an Object, if not then it is a primitive type
- An object is a collection of related data or functionality.
- Is a key value pair
- You can reference a property on an object with their key
const User = { name: "David" }
User.name;
Using Objects
- You can access properties of an object with;
- Dot Notation (.)
- Square Bracket Notation ([])
const obj = { thisIsAKey: "this is a value" };
console.log(obj);
console.log(obj.thisIsAKey);
const person = {
name: "David",
email: "@email.com",
house: {
address: "123 Street",
prevOwners: {
owners: ["Alice", "Bob", "Charlie"],
},
},
"variable with spaces": "value of variable with spaces",
displayRandomStuff() {
return "Random Stuff";
},
same: 1,
same: 2,
};
console.log(person);
console.log(person.name);
console.log(person.house.address);
console.log(person.house.prevOwners.owners[1]);
console.log(person["house"]["prevOwners"]["owners"][1]);
console.log(person["variable with spaces"]);
console.log(person.displayRandomStuff());
console.log(person.same);
Operators
- Perform certain actions on operands
- Unary (1 Operand)
typeof "Hello"
3 + 4
- Ternary (3 Operands)
- Conditional Ternary Operator, only one in Javscript
70 >= 50 ? console.log("Pass") : console.log("Fail");
20 >= 50 ? console.log("Pass") : console.log("Fail");
Keywords
- Reserved words part of the language syntax. Which means they can't be used as an identifier for variable declarations or function declarations.
- Examples;
const var = 123
Functions
- Functions in Javascript are first-class, meaning they are treated like any other variables
- You can have function declarations or function expressions
- Function Declarations are loaded into memory during creation phase
- Function expressions are not loaded into memory but assigned as undefined
- You can reference a function declaration before you declare it in code, but not for function expressions
- You can assign functions to variables
- You can pass functions as an argument (The passsed function is called a callback function)
- You can return a function from a function
- A function that returns a function or takes another function as arguments are called higher-order functions
- A function is an object with a property prototype of Function.
sumA(10, 5);
function sumA(x, y) {
console.log(x + y);
}
sumB(1, 3);
const sumB = function (x, y) {
console.log(x + y);
};
sumB(1, 3);
const sumC = (x, y) => {
console.log(x + y);
};
sumC(10, 20);
const variableFunction = () => {
console.log("Hello");
};
variableFunction();
(
() => {
console.log("Hello");
}
)();