본문 바로가기

카테고리 없음

JavaScript return 차이

(1)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>function</title>
</head>
<body>

   
    <script>

        function temp() {
            return(a + b + funca() + funcb() + or);
        }

        alert(temp() === undefined);


    </script>
</body>
</html>

(2)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>function</title>
</head>
<body>

   
    <script>

        function temp() {
            return
            (a + b + funca() + funcb() + or);
        }

        alert(temp() === undefined);


    </script>
</body>
</html>

 

1번 코드와 2번 코드는 출력값이 같을까? 

답은 다르다.  javascript 같은 경우는 인터프리터 언어라서 컴파일러(c/c++)와 달리 코드를 한줄한줄 읽는다.

그래서 1번같은 경우 not defind 에러가 뜨고

2번코드 같은 경우는  true 가 뜬다.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>function</title>
</head>
<body>

   
    <script>


        //함수의 리턴
        function plus(a,b) {
            
            return (a+b);
        }

        //alert(plus(10,20));

        document.write(plus(2,1));
        document.write('<h1><div style = "color:red">' + plus(20,10) + '</div><h1>');
        document.write('<h2><div style = "color:blue">' + plus(200,100) + '</div><h2>');




    </script>
</body>
</html>