2024.04.04
함수
<script type="text/javascript">
function check(){
document.write('호출하면 동작함<br>');
}
check();
function check2(msg){
document.write(msg + '을 좋아합니다.<br>');
}
check2('가을');
function check3(num){
return num*num;
}
var number = check3(10);
document.write('number = ' + number + '<br>');
var play = function(){
document.write('운동을 좋아해요~~<br>');
};
play();
var play2 = function(item){
document.write(item + '를 좋아해요<br>');
};
play2('축구');
var play3 = function(x,y){
return x + y;
};
var result = play3(5,8);
document.write('result = ' + result + '<br>');
function checkIt(){
alert('인라인 방식으로 함수 호출하기');
}
</script>
함수 사용시 주의사항
<script type="text/javascript">
function 함수(){
document.write('함수 A<br>');
}
function 함수(){
document.write('함수 B<br>');
}
함수();
document.write('---------------<br>');
함수2();
function 함수2(){
document.write('함수2<br>');
}
document.write('---------------<br>');
var 함수3 = function(){
document.write('함수 C<br>');
};
var 함수3 = function(){
document.write('함수 D<br>');
};
함수3();
document.write('---------------<br>');
var 함수4 = function(){
document.write('함수4');
};
</script>
지역변수와 전역변수
<script type="text/javascript">
function test1(){
var i = 10;
document.write(i + '<br>');
}
test1();
var j;
function test2(){
j = 200;
document.write(j + '<br>');
}
test2();
function test3(){
document.write(j + '<br>');
}
test3();
a = 10;
function test4(){
document.write(a + '<br>');
}
test4();
function test5(){
m = 300;
document.write(m + '<br>');
}
test5();
document.write(m + '<br>');
</script>
let
<script type="text/javascript">
for(let i=1;i<=3;i++){
document.write(i + '<br>');
}
document.write('-----------<br>');
for(var x=1;x<=3;x++){
document.write(x + '<br>');
}
document.write(x + '<---전역적으로 인식<br>');
document.write('-----------<br>');
function test1(){
let i = 100;
document.write(i + '<br>');
}
test1();
document.write('-------------<br>');
let i = 10;
function test2(){
document.write(i + '<br>');
}
test2();
document.write(i + '<---전역적으로 사용<br>');
document.write('------------<br>');
let a = '사과';
document.write('a = ' + a + '<br>');
document.write('------------<br>');
let b;
</script>
const
<script type="text/javascript">
const a = 10;
document.write(a + '<br>');
document.write('------------<br>');
function test(){
const b = 20;
document.write(b + '<br>');
}
test();
</script>
함수의 인자
<script type="text/javascript">
function test(a,b){
document.write('a = ' + a + '<br>');
document.write('b = ' + b + '<br>');
}
test();
document.write('------------<br>');
test(10);
document.write('------------<br>');
test(10,20);
document.write('------------<br>');
test(10,20,30);
</script>
나머지 매개변수
<script type="text/javascript">
function test1(...args){
document.write('args : ' + args + '<br>');
}
test1(10,20,30);
function test2(a,b,...others){
document.write('a : ' + a + '<br>');
document.write('b : ' + b + '<br>');
document.write('others : ' + others + '<br>');
}
test2(10,20,30,40,50);
</script>
return
<script type="text/javascript">
function returnTest(){
document.write('문장A<br>');
for(let i=1;i<=10;i++){
if(i == 5){
return;
}
document.write(i + '<br>');
}
document.write('문장B<br>');
}
returnTest();
document.write('프로그램 끝!!');
</script>
화살표 함수
<script type="text/javascript">
let print = (x) => {
document.write(x);
};
print(10);
document.write('<br>-----------<br>');
let print2 = (x) => {
return x + 10;
};
document.write(print2(10));
document.write('<br>-----------<br>');
let print3 = (x,y) => {
return x * y;
};
document.write(print3(3,6));
document.write('<br>-----------<br>');
let print4 = x => {
return x * 10;
};
document.write(print4(5));
document.write('<br>-----------<br>');
let print5 = (x,y) => x*y;
document.write(print5(2,3));
</script>
내부함수(클로저 현상)
<script type="text/javascript">
function f(){
let n = 123;
function g(){
document.write('n = ' + n + '<br>');
document.write('g함수 호출');
}
return g;
}
let m = f();
document.write(m + '<br>');
document.write('----------------<br>');
m();
</script>
isFinite함수
<script type="text/javascript">
let number1 = 10 / 0;
document.write('number1 = ' + number1 + '<br>');
let number2 = -10 / 0;
document.write('number2 = ' + number2 + '<br>');
if(isFinite(number2)){
document.write('0이 아닌 수로 나눔');
}else{
document.write('0으로 나눔');
}
</script>
isNaN함수
<script type="text/javascript">
let number1 = 10 / 'A';
document.write('number1 = ' + number1 + '<br>');
if(number1 == NaN){
document.write('숫자가 아님<br>');
}else{
document.write('숫자임<br>');
}
document.write('---------------<br>');
if(isNaN(number1)){
document.write('숫자가 아님<br>');
}else{
document.write('숫자임<br>');
}
</script>
parseInt(),parseFloat()
<script type="text/javascript">
let num = '1234';
let won = '1000원';
let won2 = '원1000';
document.write(Number(num) + '<br>');
document.write(Number(won) + '<br>');
document.write(parseInt(num) + '<br>');
document.write(parseInt(won) + '<br>');
document.write(parseInt(won2) + '<br>');
document.write('-----------------<br>');
let num2 = '24.56';
let dollar = '1.5$';
let dollar2 = '$1.5';
document.write(Number(num2) + '<br>');
document.write(Number(dollar) + '<br>');
document.write(parseFloat(num2) + '<br>');
document.write(parseFloat(dollar) + '<br>');
document.write(parseFloat(dollar2) + '<br>');
document.write('-----------------<br>');
let no1 = '10';
let no2 = '3.67';
document.write(parseInt(no2) + '<br>');
document.write(parseFloat(no1) + '<br>');
</script>
encoding함수
const URL = '<<a href=http://www.naver.com?test=한글입니다.'>http:
let output = '';
output += '*escape()<br>';
output += escape(URL) + '<br><br>';
output += '*encodeURI()<br>';
output += encodeURI(URL) + '<br><br>';
output += '*encodeURIComponent()<br>';
output += encodeURIComponent(URL) + '<br><br>';
document.write(output);
eval 함수
<script type="text/javascript">
let willEval = '';
willEval += 'var number = 10;';
willEval += 'alert(number);';
eval(willEval);
alert(number);
</script>
배열의 요소 추가
<script type="text/javascript">
const array = ['포도','사과'];
document.write(array + '<br>');
array[2] = '사과';
document.write(array + '<br>');
array[10] = '망고';
document.write(array + '<br>');
document.write(array[4] + '<br>');
</script>
배열의 요소 삭제
<script type="text/javascript">
const array = ['one','two','three'];
document.write(array + '<br>');
array.length = 2;
document.write(array + '<br>');
array.length = 4;
document.write(array + '<br>');
document.write('-------------<br>');
const array2 = ['서울','부산','대구','광주'];
delete array2[1];
document.write(array2 + '<br>');
document.write('-------------<br>');
const array3 = ['한국','미국','일본','중국','호주'];
array3.splice(2,1);
document.write(array3);
</script>