TIL(JS)

JavaScript

목록 보기
1/4


Today, I learn a lecture of java script that have variable, 연산자, condition, function, object, array, repeat. Let's study together.

variable

They have a variable or a constant.
Variable is variable. If you declare something, it can variable like Next sentance.

let value = 1;
console.log(value);
value = 2;
console.log(value);

Constant can't change.

Operator

They have some operator that is +, -, *, /

let a = 1 + 2 - (3 * 4) / 4;
console.log(a);

And something like strange is ++, --, +=, -=, *=, /=

let a = 1;
a++;
++a;
console.log(a);

let a = 1;
a += 3;
a -= 3;
a *= 3;
a /= 3;
console.log(a);

And They have Logical operator that is !, ||, &&

// NOT
const a = !true;
console.log(a);
//AND
const a = true && true;
console.log(a);
//OR
let t = true || false;
t = false || true;
t = true || true;

And They have comperition operator like ===, !==, >=, <=

const a = 1;
const b = 1;
const equals = a === b;
console.log(equals);
const unequals = a !== b;
console.log(equals);

Conditional

They have conditional statement that is if-else, switch-case.

// if-else
const a = 10;
if (a === 5) {
  console.log('5입니다!');
} else if (a === 10) {
  console.log('10입니다!');
} else {
  console.log('5도 아니고 10도 아닙니다.');
}

// switch-case
const device = 'iphone';

switch (device) {
  case 'iphone':
    console.log('아이폰!');
    break;
  case 'ipad':
    console.log('아이패드!');
    break;
  case 'galaxy note':
    console.log('갤럭시 노트!');
    break;
  default:
    console.log('모르겠네요..');
}

function

Function is that like play some code to one motion, like Next code.

function add(a, b) {
  return a + b;
}

const sum = add(1, 2);
console.log(sum);

Ah, arrow function is simple function

const add = (a, b) => {
  return a + b;
};

// const add = (a, b) => a + b;

console.log(add(1, 2));

Object

Object has multiple values in a name.

const dog = {
  name: '멍멍이',
  age: 2
};

console.log(dog.name);
console.log(dog.age);

And use like next sentance.

const ironMan = {
  name: '토니 스타크',
  actor: '로버트 다우니 주니어',
  alias: '아이언맨'
};

const captainAmerica = {
  name: '스티븐 로저스',
  actor: '크리스 에반스',
  alias: '캡틴 아메리카'
};

function print(hero) {
  const text = `${hero.alias}(${hero.name}) 역할을 맡은 배우는 ${
    hero.actor
  } 입니다.`;
  console.log(text);
}

print(ironMan);
print(captainAmerica);

Array

You can make a array with '[ ]'

const array = [1, 2, 3, 4, 5];
// array[0] = 1

they have order that start 0, 1, 2,...

Loop

They have a Loop code that is for, while code.

for (let i = 0; i < 10; i++) {
  console.log(i);
}

// for - of (use in Array)
let numbers = [10, 20, 30, 40, 50];
for (let number of numbers) {
  console.log(number);
}

// for - in (use in object)
const doggy = {
  name: '멍멍이',
  sound: '멍멍',
  age: 2
};

for (let key in doggy) {
  console.log(`${key}: ${doggy[key]}`);
}

While use in complicated things.

let i = 0;
while (i < 10) {
  console.log(i);
  i++;
}

Loop has break, continue

for (let i = 0; i < 10; i++) {
  if (i === 2) continue; // 다음 루프를 실행
  console.log(i);
  if (i === 5) break; // 반복문을 끝내기
}
profile
I want to be digital nomad!

0개의 댓글