유데미 - let, const and var & BasicOperator (기본 연산자)

MK·2022년 5월 3일

Udemy 강의!

목록 보기
4/12

let, const, var크게 3가지로 변수를 지정할때 사용하는 단어들인데
var은 옛날에 많이 사용했지만 지금은 버그들이 있어서 안쓰는게 좋다고 하네요.
이건 나중에 가면 또 배운다하니 대충 이정도만 알면 되고
평소에 변수를 지정할때는 "let"을 사용하고
let은 나중에 변수의 value를 바꿀 수 있지만
"const"를 사용하면 그 변수의 value는 바꿀 수가 없습니다!

그래서 바뀌면 안되는 값을 적을때는 "const"!!!!!!!!! 만 기억하면 됩니다~

😍 과제

  1. Set the value of 'language' to the language spoken where you live (some
    countries have multiple languages, but just choose one)
  2. Think about which variables should be const variables (which values will never
    change, and which might change?). Then, change these variables to const.
  3. Try to change one of the changed variables now, and observe what happens

let language;
language = "Korean";
const country = "SouthKorea";
let population = 500;
let isIsland = true;

답.


연산자로 변수를 정하고 어떻게 쓰는지 알아보겠습니다.

const ageJonas = 2037 - 1991;
const ageSarah = 2037 - 2018;
console.log(ageJonas, ageSarah);


올해 2037이라 가정하고 Jonas 와 Sarah의 나이를 계산해보겠습니다.
먼저 Jonas는 1991이고 Sarah는 2018년 생이니 각자 2037년에 나이는 46세, 19세 입니다.

근데 사람이 많아지고 숫자가 커진다면 복잡해지니
2037이라는 숫자는 now라는 변수명으로 바꿔서 만들 수도 있습니다.

const now = 2037;
const ageJonas = now - 1991;
const ageSarah = now - 2018;
console.log(ageJonas, ageSarah);


const now = 2037;
const ageJonas = now - 1991;
const ageSarah = now - 2018;

console.log(ageJonas * 2, ageJonas / 10, 2 ** 3);
//2 ** 3 means 2 to the power of 3 = 2 * 2 * 2

여기에 추가로 산수를 해봅시다.

Jonas의 나이는 46세 인 상태! 거기다 2를 곱하면 92!
Jonas의 나이는 46세 인 상태! 거기다 10알 나누면 4.6
그리고 - ** - 별표 두개는 제곱이라는 뜻이라서 2의 3제곱인 8이 나옵니다.


사람의 이름도 만들어 보자요!

const firstName = "Heung-min";
const lastName = "Son";
console.log(firstName + lastName);

이렇게 이름이 조합이 되서 나왔습니다.
근데 이렇게 되면 뭐가 성인지 뛰어쓰기를 해야겠죠?

console.log(firstName + " " + lastName);

ㅡ " " ㅡ를 사용함으로써 공백이 생겼습니다.


대입연산자를 사용해 보겠습니다!

연산자기능
+=왼쪽의 변수에 오른쪽에 있는 값을 더한다.
-=왼쪽의 변수에 오른쪽에 있는 값을 뺀다.
*=왼쪽의 변수에 오른쪽에 있는 값을 곱한다.
/=왼쪽의 변수를 오른쪽에 있는 값으로 나눈다.
%=왼쪽의 변수에 오른쪽으로 나눈값으로 나눈 나머지를 대입한다.

let x = 10 + 5; // 15
x += 10; // x = x + 10 = 25
x *= 4; // x = x * 4 = 100
console.log(x);

x라는 변수에 10 + 5를 해보았습니다. 그럼 답은 15겠죠.
이미 15가 된 'x', 거기다 10을 더해 보겠습니다. 그럼 25가 되었습니다!
25가 된 'x'에 4를 곱하면 100이 되겠죠.

x++; // x = x + 1

여기에 증감식을 써보겠습니다. ++을 쓰면 숫자 1개가 증가합니다.
아까 100 이였으니 +1은 '101'

x--;

--는 숫자 1개가 감소합니다. 아까 101이였으니 이제는 100입니다.


😃마지막으로 비교 연산자!

const now = 2037;
const ageJonas = now - 1991;
const ageSarah = now - 2018;

console.log(ageJonas > ageSarah);
console.log(ageSarah >= 18);

const isFullAge = ageSarah >= 18;

둘의 값을 비교해보면 Jonas가 Sarah보다 나이가 많으니 결과는 true.
Sarah가 18살이상이냐?의 값도 true가 됩니다.

하지만 여기서 Sarah가 2020년생이라고 하면 결과는
false가 되겠죠.

😍 과제

  1. If your country split in half, and each half would contain half the population,
    then how many people would live in each half?
  2. Increase the population of your country by 1 and log the result to the console
  3. Finland has a population of 6 million. Does your country have more people than Finland?
  4. The average population of a country is 33 million people. Does your country
    have less people than the average country?
  5. Based on the variables you created, create a new variable 'description'
    which contains a string with this format: 'Portugal is in Europe, and its 11 million
    people speak portuguese'

let country = "SouthKorea";
let continet = "Asia";
let population = 51;
let language = "Korean";

console.log(population / 2);
population++;
console.log(population);
console.log(population > 6);
console.log(population < 33);

let description =
  country +
  " is in " +
  continet +
  ", and its " +
  population +
  " million people speak " +
  language;

console.log(description);

시마이!

profile
그라타타타

0개의 댓글