4. Using JS #
Created Wednesday 08 July 2020
In the browser #
- Using the
<script>
</script>
tag.
There are 2 ways to add JS code:
- Add it as content of the
script
tag.<!-- Method 1, direct --> <body> <script> var x = 2; </script> <body>
- Add code from a
.js
file usingsrc
attribute.script
is still a content tag.<!-- Method 2, src --> <body> <script src="path"></script> <!-- Leave tag content empty --> <body>
Note:
- Keep the
script
tag at the end of thebody
tag, and never in the head. Reason: Executing/downloadingscript
content blocks the rendering process. Or, add thedefer
attribute to thescript
tag, place it anywhere now.
In the terminal #
- Write code in
.js
files. - Run them using
node file.js
in a terminal.
REPL #
- Start the REPL using
node
command.
Differences #
- In the browser, you may use objects like
window
, but this is an error in the terminal. - UI APIs like
document
are also absent.