함수 기본 구성요소
1. input 받기
2. 기능 수행
3. output 반환
현직에서 output을 반환한다, output을 return한다 라는 표현을 씀
function sayHello(input) {
let name = input;
return "Hi "+ name;
}
sayHello("Jane");
console.log(sayHello("Jane")); // "Hi Jane"
function sayHello() {
let name = "Jane";
return "Hi "+ name;
}
sayHello();
console.log(sayHello()); // "Hi Jane"
function sayHello() {
console.log("Hello, World!");
}
sayHello(); // "Hello, World!"
탐구하기1
function myNumber() {
return 10000;
}
let number1 = myNumber();
console.log("number1 : ", number1) // "number1 :" 10000
let number2 = console.log(10000); // 10000
console.log("number2 : ", number2); // "number2 :" undefined
아래에 두 함수 sayHello와 getStringLength를 작성해주세요.
1. "안녕하세요! 만나서 반갑습니다"를 콘솔창에 찍는 함수 sayHello를 만들어주세요. 그리고 해당 함수를 호출하여 실행해주세요.
2. "I LOVE WECODE"라는 문자열의 길이를 계산해서 콘솔창에 찍는 함수 getStringLength를 만들어주세요. 그리고 해당 함수를 호출하여 실행해주세요.
function sayMyName() {
console.log("김코드")
}
sayMyName()
// 1번
function sayHello() {
console.log("안녕하세요! 만나서 반갑습니다")
}
sayHello()
// 2번
function getStringLength() {
let aaa = "I LOVE WECODE";
console.log(aaa.length)
}
getStringLength()
탐구하기2
문자열의 길이를 찍는 함수1
function getStringLength() {
let aaa = "Sense and Sensibility";
return aaa.length;
}
console.log(getStringLength())
문자열의 길이를 찍는 함수2
let aaa = "Sense and Sensibility";
function getStringLength2(a) {
let abc = a.length;
console.log(abc)
}
getStringLength2(aaa)
안되는 것:
getStringLength2(aaa)
function getStringLength2(a) {
let abc = a.length;
console.log(abc)
}
let aaa = "Sense and Sensibility";
getStringLength2(aaa)
let aaa = "Sense and Sensibility";
function getStringLength2(a) {
let abc = a.length;
console.log(abc)
}
function getStringLength2(a) {
let abc = a.length;
console.log(abc)
}
getStringLength2(aaa)
let aaa = "Sense and Sensibility";
ReferenceError: Cannot access 'aaa' before initialization
at Object. (/home/runner/01-hamsuyi-jeongyiwa-hocul-songcj92/index.js:43:18)
at Module._compile (internal/modules/cjs/loader.js:999:30)
안되는 것2:
let aaa;
getStringLength2(aaa)
aaa = "I LOVE WECODE"
function getStringLength2(a) {
let abc = a.length;
console.log(abc)
}
TypeError: Cannot read property 'length' of undefined
at getStringLength2 (/home/runner/01-hamsuyi-jeongyiwa-hocul-songcj92/index.js:48:15)
at Object. (/home/runner/01-hamsuyi-jeongyiwa-hocul-songcj92/index.js:45:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)
이거 ES6 호이스팅과 관련이 있음. var로 선언했다면 다 됐을 것임.
👉 var, let, const 차이점
returnSeven을 만들어주세요. 그리고 해당 함수를 호출해서 실행해주세요.function returnSeven () {
let number = 7;
return number;
}
/*모범답안
function returnSeven () {
return 7;
}
*/
returnSeven() // 함수의 실행
getStringLength를 만들어주세요. 그리고 해당 함수를 호출해서 실행해주세요.function getStringLength() {
let aa = "I LOVE WECODE";
let length = aa.length;
return length;
}
/*모범답안
function getStringLength() {
return "I LOVE WECODE".length;
}
*/
getStringLength() //함수의 실행
name이라는 변수에 저장해서 name을 반환하는 함수 getMyName를 만들어주세요. 그리고 해당 함수를 호출해서 실행해주세요.function getMyName() {
let name = "두루마리";
return name;
}
/*모범답안
function getMyName() {
const name = "두루마리";
return name;
}
*/
getMyName() //함수의 실행
탐구하기
let, const 비교.. https://www.howdy-mj.me/javascript/var-let-const/
function myFunction(name){
...
}
let name = myFunction("Jane")
함수 선언 시 input자리에 넣는 것(name): parameter, 인자
함수 호출 시 넣는 것("Jane"): argument
function myFrined(num1, num2){
let result = num1 + num2;
return result;
}
'인자의 이름'은 함수 내부의 '인자의 이름'과 동일해야 한다
console.log(num1) // error
console.log(num2) // error
console.log(result) // error
함수 내부(중괄호{ }안)에 선언된 변수(num1, num2, result)는 함수 외부(중괄호{ }밖)에서 쓸 수 없음
getLengthOfName을 구현해주세요. function getLengthOfName(name) {
return name.length;
}
getLengthOfName(
getAge를 구현해주세요. function getAge(age) {
console.log(age);
}
getFullName을 구현해주세요. function getFullName(first,second) {
const fullName = first + second;
console.log(fullName);
return fullName;
}