DEV Community

bhanukarkra
bhanukarkra

Posted on • Edited on

Javascript Begineer

Javascript: Runs on Javascript Engine
On firefox: Spider monkey
On Chrome: V8

Node: C++ program that include chrome v8 javascript engine, hence Node can be run outside browser and hence makes up Backend.

Ecmascript vs javascript:
Ecmascript is specification and javascript is programing language that runs on echmascript specification.

DataTypes:

  1. Primitive: String, Number, boolean, null, undefined primitives are copied by value
  2. Reference type: Object, array, Function objects are copied by reference

eg:

let x=10;
let y=x;
x=20
console.log(y)//10

but
let x={value:10};
let y=x
x.value=20;
console.log(y)//{20,10}
Enter fullscreen mode Exit fullscreen mode

typeof undefined = undefined
typeof Null= object
typeof NaN=Number

Object: Object property can be changed in two ways

  1. Dot notation
  2. Array bracket

eg: 1

let person={
name:'john',
age:30
};

person.name='Seema';//dot notation
person['name']='Seema';//array bracket. This gets used in case of dynamic value
Enter fullscreen mode Exit fullscreen mode

Array: technically Array is an object, hence typeof Array=object

function: Parameter is at the time of function declaration
Argument is passed from fucntion call

Operators: arithmatic, Assignment, comparision, logical, bitwise.

Arithmatic: +,-,,/,%,*(exponentiation),++, --

Assignemnt: let x=10;
x=x+5; OR x+=5; are same

lose equality: ==
Strict equality: === (check datatype also)

conditional: Ternary operator

let points=100;
let type=points>110?'gold':'silver';
Enter fullscreen mode Exit fullscreen mode

Logical operator with non boolean values:
false||true=true
false||'abc'=abc
false||1=1

if value is not boolean, js engine make it truthy or falsy value

Falsy values: undefined, null, '', 0, false, NaN
Anthing that is not falsy is truthy

short-circuting: false||1||2= 1

if-else
Switch:

let role='guest'
switch(role){
case 'guest': console.log('Guest User');
break;

case 'moderator': console.log('Moderator user');
break;

default: console.log('default');
Enter fullscreen mode Exit fullscreen mode

Loops: For, while, do-while, for-in, for-of

for-in & for-of are used to access objects/arrays

for-in:
`const person={
name='john';
age=30;
}
for(let key in person)
console.log(key, person[key]);
//name john
// age 30

similarly for array
const colours='red','green','Blue';
for(let index in colours)
console.log(index, colours[inded])
//0 red,1 green,2 blue

For-of
for(let color of colours)
console.log(color);
//red, green, blue

with for-of loop no need to have index as in for-in.
For-of loop cannot iterate over object

Break: this is used to jump out of loop
continue: used to jump to execute loop next iteration.

Function vs method.
If a function is a part of object it is called as method.

Factory function: if function is for producing objects, it is called as factory function

In object if key and value are same, then only key can be written
like radius:radius is same as radius.

constructor function: works same to produce objects, however naming convention is used with capital first letter.
Also object can be initialized with this.

cloning an object:
`
const circle{
radius:1,
draw(){
console.log('draw')
}
};

const another={...circle};//taking each property/method of circle object and assigning to another object.
`

const message='hi'//string primitive
const another=new string('hi')//string object

(...) spread operator vs (...) rest operator
Spread operator: takes each element of array.

rest operator accommodate each parameter of function

Getters: To access properties
Setters: To mutate them

var=function scope
Let, const= block scope

This: this reference to the object that is executing the current function

Top comments (0)