js - intro

1.console:
------------
// console.time('code start');
// console.log("hello world");
// console.log({corna:'death',hello:"harry"});
// console.table({ corna: 'death', hello: "harry",marks:34})
// console.warn("this is a warning")
// console.timeEnd()


2.var,let,const:
-----------------
var: global scope
let : local / block level scope
const: cant be changed

eg:--------------- 
const c= 'hello from const';
console.log(c);

var city = "Iowa"

city="Los santos"

{
    let city="adelaid"
    console.log(city);
}

console.log(city);

3.data type(primitive data type & reference data type):
--------------------------------------------------------
3(a)Primitive
------------
let a = null (return object but actually is primitive data type)
let b = 34
let c = "him"
let d= true
let e = undefined
console.log("my name is " +  typeof a);
console.log("my name is " +  typeof b);
console.log("my name is " +  typeof c);
console.log("my name is " +  typeof d);
console.log("my name is " +  typeof e);

3(b)Reference:
-----------
array:
-------
let f= [23,45,67,45,"hello"]
console.log("my name is " +  typeof f); //object

object literals:
----------------
let b = {
    harry:4,
    yarana:9,
    urvarshi:0
}
console.log("my name is " + typeof b);

function:
----------
function findName(params) {
}
console.log("findName is " + typeof b);

date:
------
let date=new Date()
console.log( typeof date);


4.Type conversion and type coercion:
------------------------------------
let a = 34
a=String(a)
console.log(a, typeof a);


let b= String(true)
console.log(b, typeof b);


let num1 = parseInt('34.059')
let num2 = Number('34.059')
let num3 = parseFloat('34.059404050934809')
let num4 = num3.toFixed(6)
console.log(num1, num2, typeof num1, typeof num2);
console.log(num3 , typeof num3);
console.log(num4, typeof num4);

5. Templates literals:
-----------------------
var a = 'mine'
let myHtml = ` <h1> Hello the world is ${a}</h1>`

document.body.innerHTML = myHtml


6. if else in java script :
-----------------------------

let b = '20'

if (b == 20) {
    console.log("only value is matched");
}
else if(b===20) {
    console.log("type and value both match");
}
else{
console.log("age not found");
}

7. for loop:
--------------

a=[1,2,3,4,5]

for(i=0;i<a.length;i++){
    console.log(a[i]);
}

//  or

a.forEach(element => {
 console.log(element)   
});

foor loop in objects:
---------------------
let obj={
    name:"himanshu",
    sex : "M",
    age: "24",
    City: "DTG"
}
for(let o in obj){
    console.log(`The ${o} is ${obj[o]}`)
}


8.function:
-------------
function fun(a,b='zero'){
    let msg = `${a} is my ${b}`
    return msg
}
a = 'himanshu'
let b = fun(a,'idiot')
console.log(b)

// or 

const fung = function(a, b = 'zero') {
    let msg = `${a} is my ${b}`
    return msg
}
a = 'himanshu'

let b = fung(a, 'idiot')
console.log(b)


0 Comments