티스토리 뷰
들어가기
자바스크립트에서는 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
이 함수는 파라미터에서 받은 값을 더해주고 그 값을 리턴해 주는 함수이다.
'노마드코더 > 자바스크립트' 카테고리의 다른 글
#2-3 Modifying the DOM with JS (0) | 2021.08.27 |
---|---|
#2-2 JS DOM Functions (0) | 2021.08.27 |
#2-1 너의 첫번째 함수! (Your first JS Function) (0) | 2021.08.27 |
#1-10 Organizing Data with Objects (0) | 2021.08.24 |
#1-9 Organizing Data with Arrays (0) | 2021.08.24 |
댓글