function sayHi(){}
๐กarray๋ฅผ ๋ง๋ค๋ [] ์์ด์ผํ๊ณ , String(๋ฌธ์์ด)์ ๋ง๋ค๋ ""๋ฅผ ์จ์ผํ์ผ๋ฉฐ, ๋ณ์๋ฅผ ์ง์ ํ ๋๋ const, let, var์ ์ ์ธํ๋๊ฒ์ฒ๋ผ, function์ ๋ง๋ค๋ (){} ๊ดํธ๋ฅผ ๋ฃ์ด์ค์ผํ๋ค.
function sayHi(){
console.log("hello");
alert("hello");
}
sayHi();
//ํธ์ถ์ํฌ๋ ๋ง๋ค ์ฝ์๊ณผ ์๋ฆผ์ฐฝ์ "hello"๊ฐ ์ฐํ!
๐กfunction์ ๋งค๊ฐ๋ณ์(parameter)๋ฅผ ์ง์ ํ๊ธฐ ์ํด์ ()์์ ๋งค๊ฐ๋ณ์๋ช ์ ์ง์ ํด์ฃผ๋ฉด๋จ.
function sayHi(nameOfPerson, interest){
console.log("Hi my name is " + nameOfPerson + " and I am interested in " + interest)
}
sayHi("Heewon", "playing a piano");
//console์ ์ฐํ๋ ๊ฒฐ๊ณผ๊ฐ์ "Hi my name is Heewon and I am interested in playing a piano"๊ฐ ๋๋ค.
// ์ฌ๊ธฐ์ "Heewon"๊ณผ "playing a piano"๋ฑ ์ค์ ์ ๋ฌ๋๋ ๊ฐ์ ์ธ์๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ์์ด๋ก argument๋ผ๊ณ ํ๋ค.
๐กobject ์์ function๋ง๋๋ ๋ฐฉ๋ฒ!
const human = {
name: "Heewon",
sayHi: function(otherPersonsName){
console.log("hello! " + otherPersonsName + " nice to meet you!");
}
}
//์ ์ฝ๋์ ๊ฐ์ด function ๋ช
์ ๋จผ์ ์ง์ ํด์ฃผ๊ณ function์ ์จ์ ํจ์๋ฅผ ๊ธฐ๋กํ๋ค.
console.log(human.name);
human.sayHi("Hyejin");
๊ฒฐ๊ณผ : "hello! Hyejin nice to meet you!"