Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Hàm eval() trong JavaScript

Hàm eval() thực thi một chuỗi chứa mã JavaScript và trả về kết quả của đoạn mã đó. Cảnh báo: Việc sử dụng eval() gây rủi ro bảo mật nghiêm trọng (xss, injection) và ảnh hưởng hiệu năng. Nên tránh dùng khi có thể.

Cú pháp

Cú pháp của nó như sau:

eval(string)

Ví dụ

<html> <head>    <title>Ví dụ JavaScript</title> </head> <body>    <script type="text/javascript">       document.write(eval("32*45"));       document.write("<br>");       document.write(eval("'Hello welcome to Tutorialspoint'"));    </script> </body> </html>

Kết quả

1440
Hello welcome to Tutorialspoint