유튜브 강의
자바스크립트 4. 코딩의 기본 operator, if, for loop 코드리뷰 팁 | 프론트엔드 개발자 입문편 (JavaScript ES6)
// String concatenation
console.log('my' + 'cat');
console.log('1' + 2);
console.log(`string literals: 1+2=${1+2}`);
console.log("ellie's book")
// Numeric operators
console.log(2 + 3); // add
console.log(2 - 3); // substract
console.log(2 * 3); // multiply
console.log(2 / 3); // divide
console.log(2 % 3); // remainder
console.log(2 ** 3); // exponentiaion
// Increment and decrement operators
let counter = 2;
const preIncrement = ++counter;
// counter = counter + 1
// preIncrement = counter;
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`)
const postIncrement = counter++;
// postIncrement = counter;
// counter = counter + 1 ;
console.log(`postIncrement: ${postIncrement}, counter: ${counter}`)
const predecrement = --counter;
// counter = counter - 1
// preIncrement = counter;
console.log(`preIncrement: ${predecrement}, counter: ${counter}`)
const postdecrement = counter--;
// postIncrement = counter;
// counter = counter - 1 ;
console.log(`postIncrement: ${postdecrement}, counter: ${counter}`)
// Assignment operators
let x = 3;
let y = 6;
x += y; // x = x + y;
x -= y;
x *= y;
x /= y;
// Comparison operators
console.log(10 < 6); // less than
console.log(10 <= 6); // less than or equal
console.log(10 > 6); // more than
console.log(10 >= 6); // more than or equal
// Logical operators: ||(or), &&(and), !(not)
const value1 = true;
const value2 = 4 < 2;
// ||(or), finds the first truthy value
console.log(`or: ${value1 || value2 || check()}`)
//&&(and), finds the first falsy value
console.log(`and: ${value1 && value2 && check()}`)
// nullableObject && nullableObject.something
// if (nullableObject != null) {
// nullableObject.something;
// }
function check() {
for (let i = 0; i < 10; i++) {
// wasting time
console.log('🎉')
}
return true;
}
// !(not)
console.log(!value1);
// Equality
const stringFive = '5';
const numberFive = 5;
// == : loose equality, with type conversion
console.log(stringFive == numberFive);
console.log(stringFive != numberFive);
// === : strict equality, no type conversion
console.log(stringFive === numberFive);
console.log(stringFive !== numberFive);
// object equality by reference
const ellie1 = {name : 'ellie'};
const ellie2 = {name : 'ellie'};
const ellie3 = ellie1;
console.log(ellie1 == ellie2);
console.log(ellie1 === ellie2);
console.log(ellie1 === ellie3);
// equality -puzzler
console.log(0 == false);
console.log(0 === false);
console.log('' == false);
console.log('' === false);
console.log(null == undefined);
console.log(null === undefined);
// Conditional operators : if
const name = 'coder';
if (name === 'ellie') {
console.log('Welcom, Ellie!');
} else if (name === 'coder') {
console.log('You are amazing coder');
} else {
console.log('unknown');
}
// Ternary operator( ? : )
console.log(name === 'ellie' ? 'yes' : 'no');
// Switch statement
const browser = 'Firefox';
switch (browser) {
case 'IE':
console.log('go away');
break;
case 'Chrome':
case 'Firefox':
console.log('love you');
break;
default:
console.log('same all');
break;
}
// Loops
let i = 3;
while (i > 0) {
console.log(`while: ${i}`);
i--;
}
// do while
do {
console.log(`do while: ${i}`);
i--;
} while (i > 0);
// for
for (i = 3; i > 0; i--) {
console.log(`for: i: ${i}`)
}
for (let i = 3; i > 0; i = i - 2) {
// inline variable declaration
console.log(`inline variable for: ${i}`)
}
// Quiz
// Q1. iterate from 0 to 10 and print only even numbers(use continue)
for (i = 0; i <= 10; i++) {
if (i % 2 == 1) {
continue;
}
console.log(`i: ${i}`);
}
// Q2. iterate from 0 to 10 and print numbers until reaching 8(use break)
for (i = 0; i <= 10; i++) {
console.log(`i: ${i}`);
if (i == 8) {
break;
}
}
유튜브 강의
자바스크립트 5. Arrow Function은 무엇인가? 함수의 선언과 표현 | 프론트엔드 개발자 입문편(JavaScript ES6)
function log(message) {
console.log(message);
}
// parameters
function changeName(obj) {
obj.name = 'coder';
}
const ellie = {name: 'ellie'};
changeName(ellie);
console.log(ellie);
// default parameters
function showMessage(message, from = 'unknown') {
console.log(`${message} by ${from}`)
}
showMessage('Hi!');
// Rest parameters
function printAll(...args) {
for (let i = 0; i < args.length; i++) {
console.log(args[i]);
}
for (const arg of args) {
console.log(arg);
}
args.forEach((arg) => console.log(arg));
}
printAll('dream', 'coding', 'club')
// Local scope
let globalMessage = 'global'; // global variable
function printMessage() {
let message = 'hello';
console.log(message); // local variable
console.log(globalMessage)
}
printMessage();
// Return a value
function sum(a, b) {
return a + b;
}
const result = sum(1,2);
console.log(`sum: ${sum(1,2)}`);
// Early return, early exit
// bad
function upgradeUser(user) {
if (user.point > 10) {
// long logic...
}
}
// good
function upgradeUser(user) {
if (user.point <= 10) {
return;
}
// long logic...
}
// Function expression
const print = function () {
console.log('print');
}
print();
const printAgain = print;
printAgain();
const sumAgain = sum;
console.log(sumAgain(1,3));
// Callback function
function randomQuiz(answer, printYes, printNo) {
if (answer === 'love you') {
printYes();
} else {
printNo();
}
}
// anonymous function
const printYes = function() {
console.log('yes!');
};
// named function
const printNo = function print() {
console.log('no!');
};
randomQuiz('wrong', printYes, printNo);
randomQuiz('love you', printYes, printNo);
// Arrow function
const simplePrint = function () {
console.log('simplePrint');
}
const simplePrint = () => console.log('simplePrint');
const add = (a, b) => a + b;
const simpleMultiply = (a, b) => {
// do something more
return a * b;
};
// IIFE
(function hello() {
console.log('IIFE');
})();
유튜브 강의
자바스크립트 6. 클래스와 오브젝트의 차이점(class vs object), 객체지향 언어 클래스 정리 | 프론트엔드 개발자 입문편 (JavaScript ES6)
// class declarations
class Person {
// constructor
constructor(name, age) {
// fields
this.name = name;
this.age = age;
}
// methods
speak() {
console.log(`${this.name}: hello`);
}
}
const ellie = new Person('ellie', 20);
console.log(ellie.name);
console.log(ellie.age);
ellie.speak();
// Getter & Setter
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get age() {
return this._age;
}
set age(value) {
// if (value < 0) {
// throw Error('age cannot be negative')
// }
this._age = value < 0 ? 0 : value;
}
}
const user1 = new User('Steve', 'Job', -1);
console.log(user1.age);
// Public & Private
class Experiment {
publicField = 2;
#privateField = 0;
}
const experiment = new Experiment();
console.log(experiment.publicField);
console.log(experiment.privateField);
// Static
class Article {
static publisher = 'Dream Coding';
constructor(articleNumber) {
this.articleNumber = articleNumber;
}
static printPublisher() {
console.log(Article.publisher)
}
}
const article1 = new Article(1);
const article2 = new Article(2);
console.log(article1.publisher);
console.log(Article.publisher);
Article.printPublisher();
Inheritance
Diversity
// Inheritance & diversity
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
getArea() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {
draw() {
super.draw();
console.log('▲')
}
getArea() {
return (this.width * this.height) / 2;
}
}
const rectangle = new Rectangle(20,20, 'blue');
rectangle.draw();
console.log(rectangle.getArea());
const triangle = new Triangle(20,20, 'red');
triangle.draw();
console.log(triangle.getArea());
// class checking : instanceOf
console.log(rectangle instanceof Rectangle);
console.log(triangle instanceof Rectangle);
console.log(triangle instanceof Triangle);
console.log(triangle instanceof Shape);
console.log(triangle instanceof Object);
유튜브 강의
자바스크립트 7. 오브젝트 넌 뭐니? | 프론트엔드 개발자 입문편 (JavaScript ES6)
// Literals & Properties
// object = {key: value}
const obj1 = {}; // 'object literal' syntax
const obj2 = new Object(); // 'object constructor' syntax
const ellie = {name: 'ellie', age: 4};
function print(person) {
console.log(person.name);
console.log(person.age);
}
print(ellie);
ellie.hasJob = true;
console.log(ellie.hasJob);
// Computed properties
console.log(ellie.name);
console.log(ellie['name']);
ellie['hasJob'] = true;
console.log(ellie.hasJob);
function printValue(obj, key) {
console.log(obj[key]);
}
printValue(ellie, 'name');
printValue(ellie, 'age');
// property value shorthand
function makePerson(name, age) {
return {
name,
age,
}
}
const person1 = makePerson('ellie', 30)
console.log(person1);
// Constructor function
function Person(name, age) {
// this = {};
this.name = name;
this.age = age;
// return this;
}
const person2 = new Person('ellie', 30)
console.log(person2);
// in operator
console.log('name' in ellie);
console.log('age' in ellie);
console.log('height' in ellie);
// for..in vs for..of
// for (key in obj)
for (const key in ellie) {
console.log(key);
}
// for (value of iterable)
const array = [1,2,6,7];
for (const value of array) {
console.log(value);
}
// Cloning
const user = {name: 'ellie', age: '20'};
const user2 = user
console.log(user);
// old way
const user3 = {};
for (const key in user) {
user3[key] = user[key];
}
console.log(user3);
const user4 = Object.assign({}, user);
console.log(user4);
const fruit1 = {color: 'red'};
const fruit2 = {color: 'blue', size: 'big'}
const mixed = Object.assign({}, fruit2, fruit1);
console.log(mixed.color);
console.log(mixed.size);
유튜브 강의
자바스크립트 8. 배열 제대로 알고 쓰자. 자바스크립트 배열 개념과 APIs 총정리 | 프론트엔드 개발자 입문편 (JavaScript ES6 )
// Array
const arr1 = new Array();
const arr2 = [1, 2];
// Index Position
const fruits = ['apple', 'banana'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[fruits.length - 1]);
// Looping
for (let i = 0; i > fruits.length; i++) {
console.log(fruits[i])
}
for (const value of fruits) {
console.log(value)
}
fruits.forEach((fruit) => console.log(fruit))
// Addition, deletion, copy
// push
fruits.push('strawberry', 'peach');
console.log(fruits);
// pop
fruits.pop();
fruits.pop();
console.log(fruits);
// unshift
fruits.unshift('strawberry', 'peach');
console.log(fruits);
// shift
fruits.shift();
fruits.shift();
console.log(fruits);
// splice
fruits.push('strawberry', 'peach', 'lemon');
fruits.splice(1,2, 'orange')
console.log(fruits);
// combine two arrays
const fruits2 = ['pear', 'coconut']
const newfruits = fruits.concat(fruits2);
console.log(newfruits);
// Searching
console.log(fruits);
// indexOf
console.log(fruits.indexOf('apple'))
// includes
console.log(fruits.includes('apple'))
// lastIndexOf
fruits.push('apple');
console.log(fruits.indexOf('apple'))
console.log(fruits.lastIndexOf('apple'))