티스토리 뷰

노마드코더/자바스크립트

#2.1.1 More Function Fun

선물공룡디보 2021. 8. 27. 20:09

들어가기

자바스크립트에서는 console.log를 이용해 출력할때 "+"를 이용해서 변수나 문자열을 있는다.

fucntion sayHello( str,age ){ 
	console.log("Hello ! " + str  + "you have" + age + " years of age" );
}

sayHello("Nicolas", 15);

 

템플릿 문자열, 리턴 값

fucntion sayHello( str,age ){ 
	console.log(`Hello ! ${str} you have ${age} years of age`);
}

const greetNicola = sayHello("Nicolas", 15);
// Hello ! Nicolas you hane 15 years of age undefined

 

+ 나 , 대신 템플릿 문자열을 이용하면 쉽게 문자열을 출력할 수 있다. 
함수에서 return 을 하지않으면 undefined 를 반환한다.

 

메서드 함수 이용


함수를 객체에 추가하면 메서드 함수가 된다.

const calculator = {
	plus: function( a,b ){
		return a + b;
	}
}

const plus = calculator.plus(5, 5);
console.log(plus); // 5


이 함수는 파라미터에서 받은 값을 더해주고 그 값을 리턴해 주는 함수이다.

댓글