[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-5 (ํจ์ / ํ์ดํ ํจ์)
โ ํจ์
function showError(){
console.log("error");
}
showError();
function sayHello(name){
const msg = `Hello, ${name}`;
console.log(msg);
}
sayHello('Hyunjin'); // Hello, Hyunjin
sayHello('Mike'); // Hello, Mike
sayHello('Jane'); // Hello, Jane
function sayHello(name){
let msg = 'Hello';
if(name){
msg = `Hello, ${name}`;
}
console.log(msg);
}
sayHello(); // Hello
sayHello('Hyunjin'); // Hello, Hyunjin
function sayHello(name){
let msg = 'Hello';
if(name){
msg += ', ' + name;
}
console.log(msg);
}
sayHello(); // Hello
sayHello('Hyunjin'); // Hello, Hyunjin
๐ ์ฃผ์์ฌํญ ๐
msg ๋ sayHell() ํจ์ ์์์๋ง ์ฐ์ด๋ ์ง์ญ๋ณ์ โถ ํจ์ ๋ฐ๊นฅ์์ ์ฌ์ฉํ๋ฉด ์ค๋ฅ๋ฐ์
function sayHello(name){
let newName = name || 'friend';
let msg = `Hello, ${newName}`;
console.log(msg);
}
sayHello(); // Hello, friend
sayHello('Jane'); // Hello, Jane
โ ์ ์ญ๋ณ์์ ์ง์ญ๋ณ์
let msg = "Hello"; // ์ ์ญ๋ณ์
console.log(msg); // 1
function sayHello(name){
let msg = "Welcome" // ์ง์ญ๋ณ์
console.log(msg + ' ' + name); // 2
}
sayHello('Hyunjin'); // 3
console.log(msg);
// ๊ฒฐ๊ณผ
/*
1) Hello
2) Welcome Hyunjin
3) Hello
*/
let msg = "Hello";
console.log("ํจ์ ํธ์ถ ์ "); // 1
console.log(msg); // 2
function sayHello(name){
console.log("ํจ์ ๋ด๋ถ"); // 3
if(name){
msg += `, ${name}`;
}
console.log(msg); // 4
}
sayHello();
sayHello('Hyunjin');
console.log("ํจ์ ํธ์ถ ํ"); // 5
console.log(msg); // 6
// ๊ฒฐ๊ณผ
/*
1) ํจ์ ํธ์ถ ์
2) Hello
3) ํจ์ ๋ด๋ถ
4) Hello
3) ํจ์ ๋ด๋ถ
4) Hello, Hyunjin
5) ํจ์ ํธ์ถ ํ
6) Hello, Hyunjin
*/
โ ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ ๊ฐ ์ค์ ํ๊ธฐ
function sayHello(name = 'friend'){
let msg = `Hello, ${name}`;
console.log(msg);
}
sayHello(); // Hello, friend
sayHello('Jane'); // Hello, Jane
โ ๋ฐํํ ํจ์
๋ฐํ?
return์ ํตํด ๊ฐ์ ํจ์ ๋ฐ์ผ๋ก ์ ๋ฌํ๋ ๊ฒ
function showError(){
AudioListener('์๋ฌ ๋ฐ์');
}
๐ ์ฃผ์ ๐
return๋ฌธ์ด ์๋ค๊ณ ํด์ ์๋ฌด ๊ฐ๋ ๋ฐํํ์ง ์๋ ๊ฒ์ ์๋!
ํญ์ undefined๋ฅผ ๋ฐํํจ!
function showError(){
AudioListener('์๋ฌ ๋ฐ์');
return;
}
function add(num1, num2){
return num1 + num2;
}
const result = add(2,3);
console.log(result); // 5
๐ก ํจ์ ์ด๊ฒ๋ง ์๊ฐํ์!ํจ์๋ ํ๊ฐ์ง ๋์๋ง ํ๋ ๊ฒ์ด ์ข์!ํจ์์ ์ด๋ฆ์ ๋์๊ณผ ๊ด๋ จ์ด ์์ด์ผ ํจ!
โ ํจ์์ ์ฌ๋ฌ๊ฐ์ง ์ ์ธ ๋ฐฉ๋ฒ
sayHello();
function sayHello(){ // ํธ์ด์คํ
- ๋ฐ์์ ์ ์ธํ์ง๋ง ๋ฒ์๊ฐ ์๊น์ง ๊ฐ๋ฅ
console.log('Hello');
}
// Hello
ํธ์ด์คํ ์ด๋?
์ธํฐํ๋ฆฌํฐ๊ฐ ๋ณ์์ ํจ์์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ธ ์ ์ ๋ฏธ๋ฆฌ ํ ๋นํ๋ ๊ฒ
์ฆ, ํจ์ ์ ์ธ๋ณด๋ค ํธ์ถ์ ๋จผ์ ํด๋ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฒ!
(๋จ let, const์๋ ์ ์ฉ ๋ถ๊ฐ๋ฅ)
let sayBye = function() {
console.log('Bye');
}
sayBye(); // Bye
๋ง์ฝ ํจ์ ์ ์ธ๋ฌธ์ฒ๋ผ ํจ์ ํธ์ถ์ ๋จผ์ ํ๋ค๋ฉด?
sayHi(); // ์ค๋ฅ ๋ฐ์
let sayHi = function() {
console.log('Hi');
}
let showError = () =>{
console.log("error");
}
3-2. ๋งค๊ฐ๋ณ์๊ฐ ์๋ ํจ์
let add = (num, num2) =>{
num1 + num2;
};
3-3. ๋งค๊ฐ๋ณ์๊ฐ ํ๋์ผ ๊ฒฝ์ฐ ( ) ์๋ต๊ฐ๋ฅ
let sayHello = name => `Hello, ${name}`
3-4. return๋ฌธ์ด ํ ์ค์ผ ๊ฒฝ์ฐ ์ถ์ฝ ๊ฐ๋ฅ
let add = (num, num2) => num1 + num2;
โ ํจ์๋ฅผ ์ด์ฉํ ๋ค์ํ ์์
ํจ์ ํํ์์ ํ์ดํ ํจ์๋ก ๋ฐ๊ฟ๋ณด๊ธฐ
1. ๋งค๊ฐ๋ณ์๋ก ์ด๋ฆ์ ๋ฐ์์ 'Hello, ์ด๋ฆ'์ ์ถ๋ ฅํ๋ ํจ์
1) ํจ์ ํํ์
const sayHello = function(name){
const msg = `Hello, ${name}`;
console.log(msg);
};
sayHello('hyunjin') // Hello, hyunjin
let sayHello = name => {
const msg = `Hello, ${name}`;
console.log(msg);
};
sayHello('Hyunjin'); // Hello, hyunjin
2. ๋งค๊ฐ๋ณ์๋ก ์ซ์ ๋๊ฐ๋ฅผ ๋ฐ์ ๋ ์์ ํฉ์ ๋ฐํํ๋ ํจ์
1) ํจ์ ํํ์
const add = function(num1, num2){
const result = num1+num2;
return result;
}
console.log(add(1, 2)); // 3
let add = (num1, num2) => {
const result = num1 + num2;
return result;
}
console.log(add(1,2)); // 3
3) ํจ์ ์ ์ธ๋ฌธ
console.log(add(1, 2)); // 3
function add(num1, num2){
return num1 + num2;
}
'๐ STUDY > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-7 (๋ฐฐ์ด) (0) | 2022.07.14 |
---|---|
[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-6 (๊ฐ์ฒด) (0) | 2022.07.13 |
[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-4 (์กฐ๊ฑด๋ฌธ / switch ๋ฌธ / ๋ฐ๋ณต๋ฌธ) (0) | 2022.07.12 |
[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-3 ( ๋น๊ต ์ฐ์ฐ์ / ๋ ผ๋ฆฌ ์ฐ์ฐ์) (0) | 2022.07.12 |
[JavaScript] ๊ธฐ์ด๋ฌธ๋ฒ-2 (alert, prompt, confirm / ํ๋ณํ) (0) | 2022.07.12 |