Console in JavaScript is basically like a terminal of any other programming language because JavaScript runs under browser window only so that's why console operates like a terminal for JavaScript.

console.warn("This is a warning message!");
console.error("This is an error message!");
console.assert(10 == (5+6));

In JavaScript, you can print in three different ways:

  • Printing on document
    document.write("Hello World!");

  • Printing on Console
    console.log("Hello World!");

  • Printing on browser's prompt window
    alert("Welcome to my JS tutorial website!");

You can declare a variable in JavaScript by three different ways:

  • Using var is something related to old JavaScript. But it will work fine.
  • Using 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.

There are two data types in JavaScript:

  1. Primitive

    • String
      var str = "I am a String";
      console.log(str);

    • Number
      var num = 56;
      var deci = 99.99;
      console.log(num, deci);

    • Boolean
      var val1 = true;
      var val2 = false;
      console.log(val1, val2);

    • Undefined
      var undf = undefined; // Declaring explictly
      var undF;
      console.log(undf, undF);

    • Null
      var n = null;
      console.log(n);

    • Symbol
      Symbol('') // High level JavaScript


  2. Reference

    • Array
      var arr = [1, 2.2, "I Love You"];
      console.log("\nARRAY");
      console.log(arr, arr[2]);

    • Object
      var phonebook = { "Shehroz": 123, "Saad": 789, "Sameer": 456 }
      console.log("\nOBJECT");
      console.log(phonebook);

There are four types of operators in JavaScript:


  1. Arthmetic Operators
    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);

  2. Assignment Operators
    var c = a;
    c += b;
    c -= b;
    c *= b;
    c /= b;
    console.log(c);

  3. Comparison operators
    console.log(a > b);
    console.log(a < b);
    console.log(a >= b);
    console.log(a <= b);
    console.log(a != b);

  4. Logical Operators
    • AND
      console.log(false && false);
      console.log(true && false);
      console.log(false && true);
      console.log(true && true);
    • OR
      console.log(false || false);
      console.log(true || false);
      console.log(false || true);
      console.log(true || true);
    • NOT
      console.log(!false);
      console.log(!true);

In JavaScript, you can use if, else if and else to execute different conditions.

Following example demonstrates the use of using coditions in JavaScript
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")
}

There are three different types of loops in JavaScript:

  1. For Loop
    for (var i = 0; i < names.length; i++) {
    console.log(names[i]);
    }

  2. While Loop

    let j = 0;
    while (j < names.length) {
    console.log(names[j]);
    j = j + 1
    }

  3. Do-While Loop
    let k = 0;
    do {
    console.log(names[k]);
    k++;
    }
    while (k < names.length);

To declare a function in JavaScript, you need to use 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.

Don't forget to call the function with its name when you need to use it. Be sure to pass correct number of arguements!

Below example helps you demonstrate the correct use of a function in JavaScript

function avg(a, b) {
ans = (a + b) / 2;
return ans;
}

c = avg(3, 5);
console.log("Average of 3 and 5 is", c);

Following are some of the array methods used in JavaScript:

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