Intro To JavaScript Slides
Intro To JavaScript Slides
ISS Lab 2
<script> tag
In HTML, Javascript code is inserted between <script> and </script> tags.
Usually goes inside the <head> tag.
But can be placed inside <body> tag as well for faster execution.
<script src="myScript.js"></script>
• Internal:
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
• Inline:
• Writing into the browser console (will show in the terminal, used for debugging purposes)
console.log(5 + 6);
JS Variables
Use let if the variables and the type can be Use let if you want to increase the Use const if the value should not be
allowed to changed later. scope of the variable to global scope. changed.
Only has block scope. Use const if the type should not be
Variables must be declared before use. changed (Arrays and Objects)
Cannot be redeclared in the same scope.
JS Comments
function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
JS Objects
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
Declaring arrays:
• const cars = ["Saab", "Volvo", "BMW"];
• const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";
• const cars = new Array("Saab", "Volvo", "BMW");
// Create a Set
const letters = new Set();
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);