Live Class Notes
JS Basics
JavaScript
JavaScript is an interpreted, lightweight programming language. It's a webpage scripting language. It is intended for the development of network-centric applications. Node.js, Adobe Acrobat, ApacheCouchDB etc also uses javascript. It's open and cross-platform. JavaScript is a single-threaded, prototype-based, dynamic language that supports object-oriented, imperative, and declarative (e.g. functional programming) programming styles.
JS introduction
Basics of JavaScript
22/08/2022
<html>
<head>
<title>Html</title>
</head>
<body>
<p>Hey, this is javascript lessson 1</p>
<script>
var a; //declaring
a = "Hey"; // initalising
var a = 10;
console.log(a);
a = "Hello world";
var a = true;
var a = null;
console.log(a);
var a = [1, 2, 3, 4, 5]; // array
var a = {
//object
name: "Shashi",
school: "Newton",
};
var a = {
name: "newton",
town: "XYZ",
age: 100,
};
//ACCESS VARIABLES
console.log(a);
console.log(a.name);
console.log(a["name"]);
//MODIFY VARIABLES
a.name = "Random name";
a["age"] = 200;
console.log(a);
USING THE OBJECT
var car = new Object();
console.log("1.", car);
car.color = "Red";
car.type = "Hatchback";
car.price = 100;
console.log("2", car);
ARRAYS
var ages = [10, 20, 30, 40, 50, 100];
index 0 1 2 3 4
ACCESS
console.log(ages[2]);
console.log(ages[4]);
MODIFY
ages[2] = 5000;
console.log(ages);
var ages = new Array(10, 20, 30, 40, 50);
ages[5] = 60;
console.log(ages);
console.log(ages.length);
FUNCTION
CREATING A FUNCTION
function add(x, y) {
console.log(x + y);
}
add(2, 4); //CALLING A FUNCTION
function multiply(x, y) { //function declaration
return x * y;
}
var value1 = multiply(20, 40);
console.log(value1);
var add = function (a, b) { //function expression
return a + b;
};
var value = add(10, 20);
console.log(value);
</script>
</body>
</html>
23/08/2022
<html>
<head>
<title>Html</title>
</head>
<body>
<p>Hey, this is javascript lessson 1</p>
<script>
//SCOPE
// var a = 10; //GLOBAL SCOPE
// function fun() {
// var a = 20; // LOCAL SCOPE
// console.log("inside function", a);
// }
// fun();
// console.log("outside function", a);
//PARSEINT and PARSEFLOAT
// var a = 10.23234324;
// console.log(a);
// console.log(parseInt(a));
// console.log(parseFloat(a));
// console.log(a.toFixed(2));
// console.log(Math.ceil(a)); //upper value
// console.log(Math.floor(a)); //lower value
//BODMAS
// console.log(3 + 4 * 5); // 20 + 3;
// console.log(4 * 3 ** 2); // 4 * 9
// var a = b = 10;
//CONDITIONS
//BASIC IF ELSE SYNTAX
// var a = 20;
// if (a == 10) {
// console.log("a is 10");
// } else {
// console.log("a is NOT 10");
// }
// var a = 20;
// if (a == 10) {
// console.log("a is 10");
// } else if (a == 20) {
// console.log("a is 20");
// } else if (a == 30) {
// console.log("a is 30");
// } else if (a == 40) {
// console.log("a is 40");
// } else {
// console.log("All conditions failed!");
// }
//SWITCH
// var a = 40;
// switch (a) {
// case 10:
// case 50:
// console.log("a is 10 or 50");
// break;
// case 20:
// console.log("a is 20");
// break;
// case 30:
// console.log("a is 30");
// break;
// case 40:
// console.log("a is 40");
// break;
// default:
// console.log("All conditions failed!");
// break;
// }
//TERNARY OPERATORS
// var x = 200;
// var y = x == 20 ? "YES" : "NO";
// console.log(y);
// == checks only value === checks value and type of variable
//IMPLICIT TYPE COERCION (automatic type conversion) is done by ==
// var a = "10";
// if (a === "10") {
// console.log("a is 10");
// } else {
// console.log("a is NOT 10");
// }
//single line if statement
// var x = 20;
// if (x == 40) console.log(" x is 20");
// console.log(" This is 2nd statement");
//EXPLICIT TYPE CONVERSION
// var p = 23.2394032;
// console.log(parseInt(p));
// console.log(parseFloat(p));
//TYPEOF
// var x = [1, 2, 3, 4, 5];
// // console.log(typeof x);
// console.log(Array.isArray(x)); // check if a variable is an array
//COMPARISON OPERATORS: >, <, <=, >=, !=
// var a = 50;
// if (a != 5) {
// console.log("TRUE");
// } else {
// console.log("FALSE");
// }
//LOGICAL OPERATORS:
//&& - AND
// || - OR
// ! - NOT
// var a = 20;
// if (a == 20) {
// console.log("TRUE");
// } else {
// console.log("FALSE");
// }
var a = 1;
var b = 2;
var c = 0;
//In JS, 1 is true, 0 is false
// null, undefinied, [], 0, false => FALSY values
// anything other than this is TRUTHY values
var d = a && b; // 1 && 2 => true && 2
var e = c && a; // 0 && 1 => false && 1
var f = a && b && c; // 1 && 2 && 0 => true && 2 && 0
console.log(f);
</script>
</body>
</html>
24/08/2022
<html>
<head>
<title>Html</title>
</head>
<body>
<p>Hey, this is javascript lessson 1</p>
<script>
//SCOPE
// var a = 10; //GLOBAL SCOPE
// function fun() {
// var a = 20; // LOCAL SCOPE
// console.log("inside function", a);
// }
// fun();
// console.log("outside function", a);
//PARSEINT and PARSEFLOAT
// var a = 10.23234324;
// console.log(a);
// console.log(parseInt(a));
// console.log(parseFloat(a));
// console.log(a.toFixed(2));
// console.log(Math.ceil(a)); //upper value
// console.log(Math.floor(a)); //lower value
//BODMAS
// console.log(3 + 4 * 5); // 20 + 3;
// console.log(4 * 3 ** 2); // 4 * 9
// var a = b = 10;
//CONDITIONS
//BASIC IF ELSE SYNTAX
// var a = 20;
// if (a == 10) {
// console.log("a is 10");
// } else {
// console.log("a is NOT 10");
// }
// var a = 20;
// if (a == 10) {
// console.log("a is 10");
// } else if (a == 20) {
// console.log("a is 20");
// } else if (a == 30) {
// console.log("a is 30");
// } else if (a == 40) {
// console.log("a is 40");
// } else {
// console.log("All conditions failed!");
// }
//SWITCH
// var a = 40;
// switch (a) {
// case 10:
// case 50:
// console.log("a is 10 or 50");
// break;
// case 20:
// console.log("a is 20");
// break;
// case 30:
// console.log("a is 30");
// break;
// case 40:
// console.log("a is 40");
// break;
// default:
// console.log("All conditions failed!");
// break;
// }
//TERNARY OPERATORS
// var x = 200;
// var y = x == 20 ? "YES" : "NO";
// console.log(y);
// == checks only value === checks value and type of variable
//IMPLICIT TYPE COERCION (automatic type conversion) is done by ==
// var a = "10";
// if (a === "10") {
// console.log("a is 10");
// } else {
// console.log("a is NOT 10");
// }
//single line if statement
// var x = 20;
// if (x == 40) console.log(" x is 20");
// console.log(" This is 2nd statement");
//EXPLICIT TYPE CONVERSION
// var p = 23.2394032;
// console.log(parseInt(p));
// console.log(parseFloat(p));
//TYPEOF
// var x = [1, 2, 3, 4, 5];
// // console.log(typeof x);
// console.log(Array.isArray(x)); // check if a variable is an array
//COMPARISON OPERATORS: >, <, <=, >=, !=
// var a = 50;
// if (a != 5) {
// console.log("TRUE");
// } else {
// console.log("FALSE");
// }
//LOGICAL OPERATORS:
// && - AND
// || - OR
// ! - NOT
// var a = 20;
// if (a == 20) {
// console.log("TRUE");
// } else {
// console.log("FALSE");
// }
//In JS, 1 is true, 0 is false
// null, undefinied, [], 0, "", false => FALSY values
// anything other than this is TRUTHY values
//NON BOOLEAN VALUES WITH LOGICAL OPERATORS
// var a = 1;
// var b = 2;
// var c = 0;
// // var d = a && b; // 1 && 2 => true && 2
// // var e = c && a; // 0 && 1 => 0
// var f = a && b && c; // 1 && 2 && 0 => true && true && 0 => 0
// console.log(f);
//LOOP
//for , while, do...while
//FOR LOOP
// console.log("newton");
// console.log("newton");
// console.log("newton");
// console.log("newton");
// console.log("newton");
//for(initial value; condition; increment/decrement ){}
// i = 5
// for (var i = 0; i < 5; i++) {
// console.log("newton", i);
// }
// for (var i = 1; i <= 10; i++) {
// console.log("2 x", i, " = ", 2 * i);
// }
//CONCATENATION
// var first_name = "Newton";
// var last_name = "School";
// var full_name = first_name + " " + last_name + 2;
// console.log(full_name);
//WHILE LOOP
// while(condition){}
// var i = 0;
// while (i < 5) {
// console.log("Newton school");
// i++;
// }
//DO...WHILE
// do{}while();
// var i = 1000;
// do {
// console.log("newton");
// i++;
// } while (i < 5);
// var arr = ["newton", "school", "bangalore", "weather"];
// 0 1 2 3
// for (var i = 0; i < arr.length; i++) {
// console.log(arr[i]);
// }
// var nums = [20, 30, 50, 100, 200];
// var sum = 0;
// for (var i = 0; i < nums.length; i++) {
// sum = sum + nums[i];
// console.log("sum = ", sum);
// }
// console.log(sum);
// var arr = ["newton", "school", "bangalore", "weather"];
// console.log(arr.join("#"));
// for (var i = 0; i < arr.length; i++) {
// console.log(arr[i]]);
// }
//FOR..OF loop
// for (var item of arr) {
// console.log(item);
// }
// var nums = [10, 20, 30, 40, 50];
// for (var number of nums) {
// console.log(number + 100);
// }
//FOR..IN loop
// var car = {
// name: "Tata",
// color: "Red",
// wheels: 3,
// engine: "32093",
// };
// for (var x in car) {
// console.log(x, car[x]);
// }
//break;
// for (var i = 0; i < 10; i++) {
// if (i == 5) {
// break; //breaks out of the loop
// }
// console.log("Index:", i);
// }
//continue
// for (var i = 0; i < 10; i++) {
// if (i == 5) {
// continue; // skips current iteration and moves on to the next iteration
// }
// console.log("Index:", i);
// }
//INFINITE LOOP
// for (var i = 0; true; i++) {
// console.log("newton");
// }
</script>
</body>
</html>