Javascript

[JavaScript] JavaScript / function / eval() / function that recognizes a string as code

OOQ 2022. 8. 4. 08:24
728x90
SMALL

#JavaScript

#eval()

eval()

eval() is a function that recognizes a string as code.

Grammar

eval( string )

for example

eval( '2+2' )

is the computed result 4, not the string 2+2.

 

example

<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>JavaScript</title>
		<style>
			body {
				font-family: Consolas, monospace;
			}
		</style>
	</head>
	<body>
		<script>
			var jb = '2+2';
			document.write( '<p>' + jb + '</p>' );
			document.write( '<p>' + eval( jb ) + '</p>' );
		</script>
	</body>
</html>

 

728x90
LIST