FSD Module 1 Notes
FSD Module 1 Notes
2. JavaScript Basics
a. Statements
Example:
document.write(greeting);
b. Comments in JavaScript
Example:
1
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
Example:
var width = 5;
var height = 10;
var area = width * height; // 50
4. Undefined – A variable that has been declared but has not been assigned a value.
5. Null – Represents an empty or non-existent value.
4. Arrays in JavaScript
• An array stores multiple values in a single variable.
Example:
2
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
console.log(colors.length); // 3
5. Operators in JavaScript
a. Arithmetic Operators
Example:
var total = (5 + 2) * 3; // 21
Example:
Assignment Operators
3
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
var price = 5;
var quantity = 14;
var total = price * quantity;
var shipping = 7;
var grandTotal = total + shipping;
document.write(message + "<br>");
document.write("Total Cost: $" + grandTotal);
Expected Output:
8. Summary
✔ JavaScript uses statements and comments to structure code.
✔ Variables store data, and data types include numbers, strings, and Booleans.
✔ Arrays store multiple values, and elements are accessed via an index.
✔ Operators perform calculations and string manipulations.
✔ Expressions evaluate values, and assignment operators update variables.
4
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
Example:
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Output: Hello, Alice!
function square(num) {
return num * num;
}
(function() {
console.log("This runs immediately!");
})();
2. Variable Scope
• Local Scope: Variables inside a function are only accessible within that function.
• Global Scope: Variables declared outside functions can be accessed anywhere in the
script.
Example:
5
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
function showScope() {
var localVar = "I am local"; // Local
console.log(globalVar); // Accessible
}
console.log(localVar); // Error: localVar is not defined
Best Practice: Use let or const to prevent accidental global variable declarations.
3. Objects in JavaScript
• Objects store key-value pairs (properties & methods).
• Objects model real-world entities like a hotel, a user, or a car.
var car = {
brand: "Toyota",
model: "Corolla",
year: 2023
};
console.log(car.brand); // Toyota
var user = {
name: "Alice",
greet: function() {
return "Hello, " + this.name;
}
};
console.log(user.greet()); // Hello, Alice
6
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
a. Math Object
Math.round(4.7); // 5
Math.floor(4.9); // 4
Math.ceil(4.1); // 5
Math.random(); // Generates a random number between 0 and 1
b. String Object
c. Date Object
a. this in a Method
var person = {
name: "John",
sayHello: function() {
return "Hello, " + this.name;
}
};
console.log(person.sayHello()); // Hello, John
7
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
8
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
2. Comparison Operators
3. Logical Operators
Example:
4. Conditional Statements
9
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
} else {
msg = "Try again!";
}
switch (level) {
case 1: msg = "Level 1"; break;
case 2: msg = "Level 2"; break;
default: msg = "Unknown";
}
• JavaScript automatically converts data types when needed (e.g., '1' + 1 becomes
"11").
• Strict equality (===) is preferred over loose equality (==).
Example:
7. Loops in JavaScript
var i = 1;
while (i <= 5) {
console.log(i);
i++;
}
10
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
var i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
Summary
11