Getting started with Javascript


Comments

// Single line comment or Inline comment

/* Multiline comment like C or CPP */

Get Input

prompt("Enter sth: ");

Display Output

console.log("Hello World");

Data Types

  • undefined
  • null
  • boolean
  • string
  • symbol
  • number
  • object.

Object datatype

It is similar to dictionary in python. Has function hasOwnProperty() to check whether particular key exists in the Object or not.

const my_dict = {
    "key 1": value,
    key: "value 2",
}; 

Variable declaration

There are three ways to declare a variable.

var myname = "Op";
let ourName = "Limited Scope";
  • Variable declaration using let:

    Using let enables us not to override any variable within a given scope.

{
  let var_1 = "op";
  let var_1 = "lol";
}

It will raise an error, because we are trying to override the var_1, within local scope.

NOTE: Variable declared with var and let can have global or local scope.

  • Variable declaration using const:

    Value of variable declared with const can’t be changed.

const pi = 3.14;

Array in JS

  • Arrays are created using const keyword.
const myArray = [1, 2, 3]
const multDimArray = [[1, 2, 3], [4, 5, 8]]
  • Array have length function to get the length of an array
console.log(myArray.length) // without parenthesis
  • Various other array functions:
  • push() - push is used to append element at end in an array.
myArray.push(1) 
  • pop() - pop is used to pop element from end in an array.
  • shift() - shift is used to pop first element from the array.
  • unshift() - unshift is used to add element at the begining in an array.

Function’s in JS

Functions are decalared as following:

function myFunc(para1, para2)
{
    //body
}

What is == in JavaScript?

  • Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.
  • So, when you compare string with a number, JavaScript converts any string to a number.
  • An empty string is always converts to zero.
  • A string with no numeric value is converts to NaN (Not a Number), which returns false.

What is === in JavaScript?

  • === - Triple equals, is a Strict Equality comparison operator in JavaScript, which returns false for the values which are not of a similar type.
  • This operator does not performs type casting for equality.
  • If we compare 2 with “2” using ===, then it will return a false value.

If Else in JS

  • Exactly similar to C or CPP
if(condition)
{
    //Body
}
else if(condtion)
{
    //Body
}
else
{
    //Body
}  

Switch Case

  • Exactly similar to C or CPP
switch(1)
{
    case 1: ;
    case 2: ;
    default: ;
}

Loops

  • Java script supports all loops (i.e for, while, and do while) having functionalty same as c/c++.
for(var i=0; i<10; i++)
{
    //Body
}
while(condition)
{
    //Body
}
do
{
    //Body
}
while(condition);

Random Numbers

  • Javascript has Math.random() function that genrates decimal random numbers between [0, 1].
  • It also has Math.floor() function to get the floor value of an decimal number.
// To generate random number within specified range
Math.floor(Math.random()*(max-min+1))+min;
  • The parseInt() function parses a string and returns an integer.
  • It takes a second argument for the radix, which specifies the base of the number in the string.
const a = parseInt("11", 2);
//This example converts the string 11 to an integer 3

The radix variable says that 11 is in the binary system, or base 2.

Function’s in JS

Functions are decalared as following:

function myFunc(para1, para2)
{
    //body
}
  • Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions. In JavaScript, all functions are First Class Functions.
  • The functions that take a function as an argument, or return a function as a return value, are called Higher Order Functions.

Ternary Operator

function findGreaterOrEqual(a, b) {
    if (a === b) {
      return "a and b are equal";
    }
    else if (a > b) {
      return "a is greater";
    }
    else {
      return "b is greater";
    }
  }

The above function can be re-written using multiple conditional operators:

function findGreaterOrEqual(a, b) {
    return (a === b) ? "a and b are equal" 
      : (a > b) ? "a is greater" 
      : "b is greater";
}

It is considered best practice to format multiple conditional operators such that each condition is on a separate line, as shown above.