Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The three variables : ~~var~~, let and const
- // Let allows you to reassign values
- // Const cannot be changed/reassigned
- // Stick to const, unless you have to reassign values, e.g a score
- let age = 30;
- age = 31;
- const name = "Hi";
- console.log(age);
- //String, numbers, boolean, null, undefined, symbol
- const stringName = "This is a string";
- const numberName = 9;
- const decimalNumber = 9.5;
- const thisisBoolean = true;
- const x = null;
- const y = undefined;
- let z //this is undefined
- console.log (typeof stringName);
- /*Concatenation or
- Template strings (using this)*/
- const johnName = "John";
- const johnAge = 30;
- console.log(`My name is ${johnName} and I am of ${johnAge} years old`);
- const me = `My name is ${johnName} and I am of ${johnAge} years old right now`;
- console.log(me);
- /* String properties and methods
- - Properties (length) do not have parentheses ()
- - Methods do have parentheses ()*/
- const txt = "Hello World!";
- console.log(txt.length);
- console.log(txt.toUpperCase());
- console.log(txt.toLowerCase());
- console.log(txt.substring(0, 5));
- //combining strings
- console.log(txt.substring(0, 5).toLowerCase());
- //splitting strings to arrays
- console.log(txt.split(""));//this splits by letter
- const tagMe = "item1, item2, item3";
- console.log(tagMe.split(", "));
- /* Arrays: 23: 54, variables that hold multiple values*/
- const fruits = ["orange", "grapes", "banana"];
- fruits[3] = "apple"; /*(Specifically access, rewrite and add, but not recommended)*/
- fruits.push("mangoes", "peach");//add at the end (1)
- fruits.unshift("strawberries");//add at the start (2)
- fruits.pop(); //remove item at the end of an array (3)
- fruits.shift(); //remove at the start (4)
- console.log(fruits);
- console.log(Array.isArray(fruits));//checks if this is an array
- console.log(fruits.indexOf("orange"));
- //========
- /*Object literals (30:10) (Key value pairs)*/
- const person = {
- firstName: "Jon",
- lastName: "Doe",
- age: 30,
- hobbies: ["Sports", "Swimming", "Reading"],
- address: {
- street: "50 main city",
- city: "Simei",
- state: "MA",
- }
- }
- person.email = "John@gmail.com";//directly adding properties
- console.log(person);
- console.log(person.hobbies[2]);//access specific within array, REFER TO THIS
- console.log(person.firstName, person.lastName) //access specific values
- console.log(person.address.street) //access values within an object's object
- //destructuting, using object literals to be actual variables
- /*const {firstName, lastName} = person;
- console.log(firstName);
- const {address: {city}} = person;
- console.log(city); //If within embedded object*/
- const {firstName, lastName, address: {city}} = person;
- console.log(city);
- /*array of objects (34:22)*/
- const todos = [
- {
- id: 1,
- text: "Take out trash",
- isCompleted:true,
- },
- {
- id: 2,
- text: "See boss",
- isCompleted:true,
- },
- {
- id: 3,
- text: "Check time",
- isCompleted:false,
- },
- ];
- console.log(todos[1].text);
- /*JSON, a data format, in 36:10*/
- const todoJSON = JSON.stringify(todos);
- console.log(todoJSON);
- /*Loops 37:49*/
- const loopers = [
- {
- id: 1,
- text: "FirstLoop",
- isCompleted:true,
- },
- {
- id: 2,
- text: "SecondLoop",
- isCompleted:true,
- },
- {
- id: 3,
- text: "Thirdloop",
- isCompleted:false,
- },
- ];
- //for
- for(let i = 0; i < 10; i++) {//parameters: assignment of iterator/variable (i = 0); condition that needs to be met; increment
- console.log(i)
- console.log(`This is my For Loop Number ${i}`)
- }
- //<= => less than/more than or equal to
- //While
- let i = 0;
- while(i < 10) {
- console.log(`while loop number: ${i}`);
- i++;
- }
- const delta = [
- {
- id: 1,
- text: "FirstLoop1",
- isCompleted:true,
- },
- {
- id: 2,
- text: "SecondLoop2",
- isCompleted:true,
- },
- {
- id: 3,
- text: "Thirdloop3",
- isCompleted:false,
- },
- ];
- //looping through arrays (40:33, 41:30)
- for(let del of delta) {
- console.log(del);
- console.log(del.text)
- console.log(del.id);
- }
- //high order array methods, suggested for any kind of array iteration (42:27)
- /* For Each (loops through them)*/
- delta.forEach(function(nameMeAnything) {
- console.log(nameMeAnything.text);
- });
- /*Map (Creates a new array from an existing array, also return the said array (43:35*/
- const deltaText = delta.map(function(nameMeAnything) {
- return nameMeAnything.text;
- });
- console.log(deltaText);
- /*Filter (Creates an array based on a condition) 44:35*/
- const deltaText2 = delta.filter(function(nameMeAnything) {
- return nameMeAnything.isCompleted === false;
- });
- console.log(deltaText2);
- /*Chaining/stacking on other array methods 45:31*/
- const deltaTextStack = delta.filter(function(nameMeAnything) {
- return nameMeAnything.isCompleted === true;
- }).map(function(todo) {
- return todo.text;
- })
- console.log(deltaTextStack);
- /*Conditionals (46:33)*/
- const x2 = 20;
- if(x2 == 4) {
- console.log("X is 10");
- } else if(x2 > 10) {
- console.log("x is greater than 10");
- } else {
- console.log("X is NOT 10");
- }
- /*Multiple conditions (49:20)*/
- const x3 = 4;
- const y3 = 11;
- if(x > 5 || y > 10) { //or
- console.log("X3 is 5 and above while Y3 is 11 and above")
- }
- if(x > 8 && y > 15) { //and
- console.log("X3 is 5 and above while Y3 is 11 and above")
- }
- /*Ternary operator, aka shorthand if statements (51:25)*/
- const tern = 11;
- const trueOrFalse = tern > 10 ? 'Then this is true' : "Else this is false";
- console.log(trueOrFalse);
- /*Switches, another way to evaluate conditions (52:20)*/
- const switcher = 10;
- const colors2 = switcher > 10 ? "red":"blue"; //red is for 11+, blue is 10 and lower
- switch(colors2) {
- case "red":
- console.log("color is red");
- break;
- case "blue":
- console.log("color is blue");
- break;
- default: //doesn't match any of the above
- console.log("color is NOT red or blue");
- break;
- }
- /*Functions (54:26)*/
- function addNumstest(num1, num2) { //inside parentheses, put parameters
- console.log(num1 + num2);
- }
- addNumstest (5,4); //num1: 5, num2: 4, leave this blank if numbers not assigned
- //setting default values for parameters
- function addNumstest2(num3 = 1, num4 = 4) { //inside parentheses, put parameters
- return num3 + num4;
- }
- console.log(addNumstest2());
- //turning to arrow function (56:39)
- const addNumsArrowtest2 = (num5 = 10, num6 = 4) => { //inside parentheses, put parameters
- return num5 + num6;
- }
- console.log(addNumsArrowtest2());
- //object-oriented programming (59:27)
- //practice
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement