Javascript - (2)

송민혁·2022년 1월 30일
post-thumbnail

변수 (let, const)

let vs const
값(데이터)의 재할당 가능! vs 값(데이터)의 재할당 불가능!

예약어

예약어란 특별한 의미를 가지고 있어, 변수나 함수 이름 등으로 사용할 수 없는 단어를 의미한다.

let this = 'Hello'; //this는 예약어이므로 SyntaxError(에러)가 뜰 것이다.
let if = 123; ////if는 예약어이므로 SyntaxError(에러)가 뜰 것이다.

함수 (function)

//함수 선언
function helloFunc() {
// 실행 코드  
  console.log(1234);
}

// 함수 호출
helloFunc(); // 1234

함수를 내가 직접 선언할 수 있고, 여러 실행 코드들로 구성할 수 있다.

반환하기 함수 (return)

function returnFunc() {
  return 123; // 데이터를 함수의 밖으로 빼낸다고 이해했음. 
}

let a = returnFunc();

console.log(a); //123

=====================================================

function sum(a, b) { //a와 b는 매개변수(Parameters)
  return a + b;
}

let a = sum(1, 2); //1과 2는 인수(Arguments) 
let b = sum(7, 12);
let c = sum(2, 4);

console.log(a, b, c); // 3, 19, 6

기명함수 vs 익명함수

기명(이름이 있는) 함수

function hello() {
}

익명(이름이 없는) 함수

let world = function () { 
}

함수를 데이터처럼 사용

const song = {
  name = 'Song',
  age = 26,
  // 메소드(Method)
  getName: function () {
    return this.name; // this는 객체를 가리킨다.
  }
};

const hisName = song.getName();
console.log(hisName); // Song
console.log(song.getName()); // Song

조건문

조건의 결과에 따라 다른 코드를 실행하는 구문 (if, else)

let isShow = true;

if (isShow) {
  console.log('Show'); //true일 경우
} else {
  console.log('Hide?');//false일 경우
}

0개의 댓글