console.warn("This is a warning message!");
console.error("This is an error message!");
console.assert(10 == (5+6));
document.write("Hello World!");
console.log("Hello World!");
alert("Welcome to my JS tutorial website!");
var
is something related to old JavaScript. But it will work fine.
let
is good because it has good memory management. The scope of let
is temporary and can only be used within {}.
const
lets you create a constant variable whose value cannot be altered.
var str = "I am a String";
console.log(str);
var num = 56;
var deci = 99.99;
console.log(num, deci);
var val1 = true;
var val2 = false;
console.log(val1, val2);
var undf = undefined; // Declaring explictly
var undF;
console.log(undf, undF);
var n = null;
console.log(n);
Symbol('') // High level JavaScript
var arr = [1, 2.2, "I Love You"];
console.log("\nARRAY");
console.log(arr, arr[2]);
var phonebook = {
"Shehroz": 123,
"Saad": 789,
"Sameer": 456
}
console.log("\nOBJECT");
console.log(phonebook);
var a = 99.9;
var b = 56.56;
console.log("The value of a + b is", a + b);
console.log("The value of a - b is", a - b);
console.log("The value of a * b is", a * b);
console.log("The value of a / b is", a / b);
var c = a;
c += b;
c -= b;
c *= b;
c /= b;
console.log(c);
console.log(a > b);
console.log(a < b);
console.log(a >= b);
console.log(a <= b);
console.log(a != b);
console.log(false && false);
console.log(true && false);
console.log(false && true);
console.log(true && true);
console.log(false || false);
console.log(true || false);
console.log(false || true);
console.log(true || true);
console.log(!false);
console.log(!true);
if
, else if
and else
to execute different conditions.
var a = 10;
var b = 10;
console.log("\nValue of a is", a, "\nValue of b is", b, "\nAnswer is ")
if (a == b){
console.log("Both values are equal!")
}
else{
console.log("Both values are not equal!")
}
if (a < b){
console.log("Yes! a is less than b");
if (b > a){
console.log("Obviously! b is greater than a");
}
}
else if (a > b){
console.log("No a is greater than b")
}
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}
let j = 0;
while (j < names.length) {
console.log(names[j]);
j = j + 1
}
let k = 0;
do {
console.log(names[k]);
k++;
}
while (k < names.length);
function
keyword along with name of the function and
with n number of parameters or arguements it accepts. Simply use {}
to write down inside the function.
Use return
keyword to return a value from a function.
function avg(a, b) {
ans = (a + b) / 2;
return ans;
}
c = avg(3, 5);
console.log("Average of 3 and 5 is", c);
MyArr = [1, 2, 3];
MyArr.length; // Check length of the array
MyArr.shift(); // Removes first element from the array
MyArr.pop(); // Removes last item from the array
MyArr.push(4); // Adds new item to the array
MyArr.unshift(0); // Adds new item at first index of an array
MyArr.sort() // Sorts an array (sorts an array alphabetically)
MyArr.toString() // Returns an array as a complete string
break // To terminate the whole loop
continue // To skip an iteration