1. Function 사용법
1-1) Function이 없는 경우
// Function이 없는 경우
console.log("Hello my name is Nico.");
console.log("Hello my name is Ssu");
console.log("Hello my name is Simon.");
…
1-2) Function이 있는 경우
// function이 있는 경우
function sayHello() {
console.log("Hello my name is");
}
// sayHello를 실행될 때마다 console.log가 실행됨.
sayHello();
sayHello();
sayHello();
Console
2. Function 데이터 받기
2-1) 인수(argument)가 하나일 때
// 인수로 어떤 데이터가 들어오면
// nameOfPerson이라는 변수 이름을 쓰게 되는 거임.
function sayHello (nameOfPerson) { // 인수는 두 개도 됨.
console.log(nameOfPerson);
}
sayHello("nico");
sayHello("dal");
sayHello("lyn");
Console
2-2) 인수가 두 개 일때
function sayHello (nameOfPerson, age) {
console.log("Hello My name is" + nameOfPerson + "and I'm " + age);
}
sayHello("nico", 12);
sayHello("dal", 14);
sayHello("lyn", 24);
Console
3. Function Return
3-1) Return 타입이 Integer일 때
const age = 96;
function calculateKrAge(ageOfForeigner) {
return ageOfForeigner + 2;
}
const krAge = calulateKrAge(age);
console.log(krAge);
Console
3-2) Return 타입이 String일 때
const age = 96;
function calculateKrAge(ageOfForeigner) {
ageOfForeigner + 2;
return "hello";
}
const krAge = calulateKrAge(age);
console.log(krAge);
Console
4. Document Object
4-1) Document : 브라우저에 이미 존재하는 Object
console에 document를 입력하면 작성한 HTML을 가져올 수 있음.
4-2) 자바스크립트
document.title = "Hello From JS!!!";
4-3) HTML
4-4) View
5. HTML in Javascript
5-1) HTML
<html>
<body>
<h1 id="title">Grab me!!!</h1>
<script scr="app.js"></script>
</body>
</html>
5-2) Javascript
document.getElementById("title");
Console
5-3) dir(object) 메소드
const title = document.getElementById("title");
console.dir(title);
Console