A JavaScript Engine is a program that:
Reads your JS code
Converts it into machine code
Executes it
Popular JS Engines V8 (Chrome, Node.js) SpiderMonkey (Firefox) JavaScriptCore (Safari)
How Engine Works
Steps: You write JS code Engine parses code Converts to machine code Executes instantly
Runtime (Browser Environment)
JavaScript alone = language
But it needs a runtime to run
Browser = Runtime Browser gives:
DOM (Document Object Model) APIs (fetch, setTimeout, etc.) Event system Example console.log("Hello");
console.log() is NOT pure JS
It is provided by the browser runtime
Runtime Components Includes: Call Stack Web APIs Event Loop Callback Queue 👉 You’ll learn these deeply later (advanced level)
Flow: Browser reads HTML Builds DOM Finds <script> Stops HTML parsing ⛔ Executes JS Continues loading
JS is blocking by default
That means:
If JS takes time → page load slows Use:
<script defer src="script.js"></script>
👉 defer = run JS after HTML loads
The <script> Tag In HTML, JavaScript code is inserted between <script> and </script> tags.
JavaScript in <head> or <body> You can place any number of scripts in an HTML document.
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
JavaScript in <head> In this example, a JavaScript function is placed in the <head> section of an HTML page.
Example
function myFunction() {
document.getElementByI HTML Sandbox — click to edit JavaScript in <body> In this example, a JavaScript function is placed in the <body> section of an HTML page.
Demo JavaScript in Body
A Paragraph
Try it
function myFu HTML Sandbox — click to edit
External JavaScript Scripts can also be placed in external files:
External file: myScript.js function myFunction() {
document.getElementById("demo" ).innerHTML = "Paragraph changed." ;
}
External scripts are practical when the same code is used in many different web pages.
JavaScript files have the file extension .js .
To use an external script, put the name of the script file in the src (source) attribute of a <script> tag:
Example < script src ="myScript.js" >< /script >
Advantages Placing scripts in external files has some advantages:
It separates HTML and code It makes HTML and JavaScript easier to read and maintain Cached JavaScript files can speed up page loads
JavaScript Display Possibilities JavaScript can "display" data in different ways:
Writing into an HTML element, using innerHTML or innerText. Writing into the HTML output using document.write(). Writing into an alert box, using window.alert(). Writing into the browser console, using console.log(). Using innerHTML To access an HTML element, you can use the document.getElementById(id) method.
Use the id attribute to identify the HTML element.
Then use the innerHTML property to change the HTML content of the HTML element:
My First Web Page
My First Paragraph
document.getElement HTML Sandbox — click to edit Using innerText To access an HTML element, use the document.getElementById(id) method.
Then use the innerText property to change the inner text of the HTML element:
My First Web Page
My First Paragraph
document.getElement HTML Sandbox — click to edit
Use innerHTML when you want to change an HTML element.
Use innerText when you only want to change the plain text.
Using document.write() For testing purposes, it is convenient to use document.write():
My First Web Page
My first paragraph.
document.write(5 + 6 HTML Sandbox — click to edit Using window.alert() You can use an alert box to display data:
My First Web Page
My first paragraph.
window.alert(5 + 6); HTML Sandbox — click to edit Using console.log() For debugging purposes, you can call the console.log() method in the browser to display data.
console.log(5 + 6); HTML Sandbox — click to edit