2023 JavaScript 111 555
2023 JavaScript 111 555
The Document Object Model (DOM) provides interfaces for interacting with elements on web pages
The Browser Object Model (BOM) provides the browser API for interacting with the web browser.
The JavaScript engine in the web browser executes the JavaScript code.
JavaScript engines were implemented as interpreters.
New JavaScript engines are typically implemented as just-in-time compilers that compile JavaScript code to bytecode for
improved performance.
Client-side vs. Server-side JavaScript
JavaScript can run on both web browsers and servers
When JavaScript is used on a web page, it is executed in web browsers, herw JavaScript works as a client-side language.
Server-side JavaScript executes on the server that allows you to access databases, file systems, etc. A popular JavaScript
server-side environment is Node.js.
JavaScript overview
To define a variable in JavaScript, you use var keyword. For example:
var x = 10;
var y = 20;
ES6 added a new way to declare a variable with the let keyword:
let x = 10;
let y = 20;
There are differences between var and let.
It’s a good practice to use the let keyword to declare variables.
function divide(a, b) {
if(b == 0) {
throw 'Division by zero';
}
return a / b;
}
Array
To declare an array, syntax:
let items = [];
To declare an array with some initial elements,
let items = [1, 2, 3];
To access the number of elements in the items array using length property
console.log(items.length); // 3
1
2
To iterate over the elements of the items array, you use the for loop statement
for(let i = 0; i < items.length; i++) {
console.log(items[i]);
}
Or use the for...of loop in ES6:
for(let item of items) {
console.log(item);
}
How to embed JavaScript code into an HTML page.
To insert JavaScript into an HTML page, you use the <script> element.
There are two ways to use the <script> element in an HTML page:
Embed JavaScript code directly into the HTML page. (or)
Reference an external JavaScript code file.
Embed JavaScript code in an HTML page
Placing JavaScript code inside the <script> element directly
This is not recommended and should be used only for proof of concept or testing purposes.
The JavaScript code in the <script> element is interpreted from top to bottom.
For example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Hello World Example</title>
<script>
alert('Hello, World!'); // alert() function to display the Hello, World! message.
</script>
</head>
<body>
</body>
</html>
Include an external JavaScript file
First, create a file whose extension is .js e.g., app.js and place it in the js subfolder.
Note that placing the JavaScript file in the js folder is not required however it is a good practice.
Then, use the URL to the JavasScript source code file in the src attribute of the <script> element.
Example
The contents of the app.js file
alert('Hello, World!');
And the following shows the helloworld.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Hello World Example</title>
<script src="js/app.js"></script>
</head>
<body>
</body>
</html>
2
3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Hello World Example</title>
</head>
<body>