Chapter 1 Overview of Javascript 2
Chapter 1 Overview of Javascript 2
Client-side Scripting
using JavaScript
• JavaScript enhances the functionality of websites, making them interactive and user-
friendly.
• It allows developers to:
• Manipulate HTML and CSS to update the user interface dynamically.
• Validate user input before sending data to the server.
• Create interactive features such as image sliders, pop-ups, and form validations.
• Fetch data from servers using APIs (e.g., AJAX) without reloading the page.
• Build complex applications like single-page applications (SPAs) and progressive web
apps (PWAs).
• Server-side and client-side refer to the location where certain tasks or processes are carried
out in a web application.
• Server-side processes are executed on the web server, while client-side processes are executed
on the user's device.
• Server-side processes have access to the server's resources, such as its CPU, memory, and
storage, as well as any databases or other servers that the web application uses.
• Client-side processes, on the other hand, have access only to the resources of the user's
device, such as its CPU, memory, and storage.
• Because server-side processes are executed on the web server, they are typically more secure
and less vulnerable to tampering or malicious attacks.
• Client-side processes, on the other hand, are executed on the user's device, which means that
they are potentially less secure and more susceptible to tampering or attacks.
• Server-side and client-side refer to the location where certain tasks or processes are
carried out in a web application.
• Server-side processes are executed on the web server before the web application is
delivered to the user's device.
• Client-side processes are executed on the user's device after the web application is
delivered.
• Server-side processes have more access to resources and are more secure, while
client-side processes have less access to resources and are potentially less secure.
• JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in
1997.
• ECMAScript is the official name of the language.
• ECMA is European Computer Manufacturers Association
• ECMAScript versions have been abbreviated to ES1, ES2, ES3, ES5, and ES6.
• Since 2016, versions are named by year (ECMAScript 2016, 2017, 2018, 2019, 2020).
• https://www.w3schools.com/js/js_versions.asp
• This attribute allow you to specify when an external script should be loaded.
• Ensures that the script is executed after the HTML document has been fully parsed.
This is used only for external scripts.
• The <noscript> tag in HTML is used to provide alternative content for users whose
browsers do not support JavaScript or have JavaScript disabled.
• This ensures that the website remains accessible and usable even when JavaScript is
not available.
Step 1: Set Up Your HTML Document - First, create a basic HTML document. This
document will serve as the structure for where you will insert your JavaScript.
Step 2: Add the <script> Tag - JavaScript is inserted into an HTML document using the
<script> tag. This can be done in three main ways:
1. Inline
2. Internal
3. External
Place JavaScript code within the <script> tag in the HTML document's head or body section.
Link an external
JavaScript file using the
<script> tag's src
attribute. This keeps your
JavaScript code separate
from your HTML, making
it easier to manage.
script.js
var x = ""; //This creates a variable called x and assigns empty string to it
var cheese = "yummy"; //This creates a variable called cheese and assigns it a value "yummy"
var age = 25; //This creates a variable called age and assigns it a value of 25
//Invalid variables
var 1cat = ""; //can not start with number
var $cat = ""; //$ is used by libraries
var my-cat = ""; // - is not allowed
var my cat = ""; //space between variable name
var new = ""; //new is a keyword
//Types of variables
//JavaScript is loosely typed. var myNumAsString = "5";
var mynum = 10;
var notNum = "5"; typeof(mynum) //'number'
typeof(myNumAsString) //'string'
var newNum = 10; var undefined; //undefined
typeof(undefined) //'undefined'
newNum + notNum //'105' var nothing = null; //undefined
typeof(nothing) //'object'
var emptyString = "";
typeof(emptyString); //'string'
• Single-line Comments
• Single-line comments start with //. Any text following // on the same line is
considered a comment and is ignored by JavaScript.
• Multi-line Comments
• Multi-line comments start with /* and end with */. Everything between /* and */ is
considered a comment and is ignored by JavaScript.
{
let x = 2;
}
// x can NOT be used here
Client-side scripting using JavaScript 32
JavaScript 'var' : Function Scope
{
var x = 2;
}
// x CAN be used here
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
• The object data type can contain both built-in objects, and user defined objects:
• Built-in object types can be:
• objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and more.
• In JavaScript, output refers to the process of displaying data to the user or to another
system.
• There are several ways to produce output in JavaScript, and the method you choose
depends on the context and the desired output destination.
• The console.log() method is used to print messages to the web console. This is
primarily used for debugging purposes.
console.log("Hello, World!");
let x = 10;
console.log("The value of x is:", x);
• The alert() method displays an alert dialog with a specified message and an OK button.
alert("Hello, World!");
let x = 10;
alert("The value of x is: " + x);
• The document.write() method writes directly to the HTML document. It is often used for
testing purposes and should be avoided in modern web development.
document.write("Hello, World!");
let x = 10;
document.write("The value of x is: " + x);
• The innerHTML property sets or returns the HTML content of an element. This method is
commonly used to update the content of a web page dynamically.
<p id="output"></p>
<script>
document.getElementById("output").innerHTML = "Hello, World!";
let x = 10;
document.getElementById("output").innerHTML += "<br>The value of x is: " + x;
</script>
• The prompt() method displays a dialog box that prompts the user for input and returns the
input value.
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• String Operators
• Logical Operators
• Bitwise Operators
• Ternary Operators
• Type Operators
` ` OR
^ XOR 5 ^ 1 // 4
~ NOT ~5 // -6
• The ternary operator, also known as the conditional operator, is a concise way to
perform conditional operations in JavaScript.
• It evaluates a condition and returns one of two values based on whether the
condition is true or false.
• Mark and John are trying to compare their BMI (Body Mass Index), which is calculated using the formula: BMI = mass
/ (height * height) (mass in kg and height in meters).
• Your task is to write some code to help them:
• Store Mark's and John's mass and height in variables called massMark, heightMark, massJohn and heightJohn.
• Calculate both their BMIs using the formula, and store the results in two variables called BMIMark and BMIJohn.
• Log the value of BMIMark and BMIJohn to the console.
• Create a boolean variable markHigherBMI containing information about whether Mark has a higher BMI than John.
Log it to the console too
• TEST DATA 1: Marks weighs 78 kg and is 1.69 m tall. John weighs 92 kg and is 1.95 m tall.
• TEST DATA 2: Marks weights 95 kg and is 1.88 m tall. John weights 85 kg and is 1.76 m tall.
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
}
• The if...else statement executes one block of code if a condition evaluates to true, and
another block of code if it evaluates to false.
let x = 3;
if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is less than or equal to 5");
}
• The else if statement allows you to check multiple conditions by adding more
conditional blocks.
if (x > 10) {
console.log("x is greater than 10");
} else if (x > 5) {
console.log("x is greater than 5 but less than or equal to 10");
} else {
console.log("x is 5 or less");
}
• Use the BMI example from Challenge #1, and the code you already wrote, and
improve it:
• 1. Print a nice output to the console, telling the user who has the higher BMI.
The message can be either:
• "Mark's BMI is higher than John's!" or "John's BMI is higher than
Mark's!".
• 2. Modify the outputs above to use template literals to include the BMI values
in the outputs.
• Example: "Mark's BMI (28.3) is higher than John's (23.9)!" or "John's
BMI (29.1) is higher than Mark's (27)!" .
• The for loop is used when you know in advance how many times you want to execute
a statement or a block of statements.
• It consists of three parts: initialization, condition, and increment/decrement.
for (let i = 0; i < 5; i++) { var colors = ['red', 'blue', 'yellow', 'green'];
console.log(i);
} for(var i=0; i<colors.length; i++){
console.log(colors[i]);
}
const person = {
firstName: "John",
lastName: "Doe",
age: 25
};
• The for...of loop is used to iterate over the values of an iterable object, such as an
array, string, or other collections.
• The while loop is used when the number of iterations is not known beforehand and
the loop should continue running as long as the specified condition is true.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
• The do...while loop is similar to the while loop, but it ensures that the loop body is
executed at least once before the condition is tested.
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);