Labels

Tuesday, January 24, 2023

JavaScript notes with code for beginners

 // This is a single line comment

/* 

This is a multi line comment 

*/

console.log("Hellow world")

// variable and constant

let l=98; // new methode

var b=65; // old mehode

const pi = 3.14;

console.log("The area of Rectangle = ",l*b)

/*

premitive data types : (nn ss bb u )

Number, null, string, Symbol, BigInt, boolean, undefined

*/

let v1= 34 ;// number

let v2 = null 

let v3 = "It is a string";

let v4 = Symbol("It is a symbol ")

let v5 = BigInt(454)

let v6 = true;

let v7 = undefined;

console.log(v1,v2,v3,v4,v5,v6,v7)


/*

Objects ( just like dictionary in python but i is not dictionary )

*/

let student = {  // we can create a constant object by writing const instead of let

    "Mohan" : "Rank 1",

    "Harry" : true,

    "Sumit" : null,

    "Rohan" : undefined,

    "Amit"  : 34,

}


console.log(student["Mohan"],student["Harry"],student["Sumit"],student["Rohit"])


/*

Arithematic Operator : + , - , * , / , ++ , -- , ** (for power)

conditional operator :  == , === , > , < , >= , <= , !=

Assiment operator : = , += , -= , *= , /= , 

*/


// conditions : if else ladder


// let age =prompt("What is your age : ")

// parseInt(age);

let age = 34

if (age >= 18) {

    console.log("You can give vote.")

    // alert("You can give vote.")

}


else if (age >=0){

    // alert("You can't give vote.")

    console.log("You can't give vote")

}

else{

    console.log("Not a valid input.")

}


// loops 

// While loop

let num=5;

let fact=1;

while (num--){

    fact=fact*num;

}

console.log("The facorial of 5 : ",fact)


// for loop

let arr=[];

for (let index = 0; index < 6; index++) {

    arr[index]=index % 3;

}


console.log("The length of array arr : ",arr.length)

console.log("all elements of array arr[] :")

for (let index = 0; index < arr.length; index++) {

    console.log(arr[index])

}


/* Functions in js : 

There are 3 ways of writing a function in JavaScript:

1. Function Declaration.

2. Function Expression.

3. Arrow Function.

*/


// Function declaration

function add(a, b) {

console.log(a + b);

}


// Calling a function

add(2, 3);



// Function Expression

const modulo = function(a, b) {

console.log(a+b);

}


// Calling function

modulo(2, 3);


// Arrow funtion 

const great = (a, b) => {

    if (a > b) 

        return "a is greater";

    else

        return "b is greater";

}

  

console.log(great(3,5));


/* Strings in js :

 */

let str1 = "This is a String."

let str2 = "The world is realy good."

console.log(typeof(str1))

let strCat = str1+" "+str2;

console.log(strCat)


/* Array in js  */

// array declaration


let arr1 = [];

for(let i=0;i<6;i++){

    arr1[i]=i**2; // i*i

}


let arr2 = ["abc","mango","sumit"];

let arr3 = [2,5,3,12,4];

console.log(arr2[0])

console.log(arr3[3])

No comments:

Post a Comment

How to get free course on Coursera with cirtificate ? , coursera Financial aid questions & answers :

Coursera Financial aid questions & answers   Q1) Why are you applying for Financial Aid? ans:-    I am in my second year of B.Tech in ...