DEV Community

Vishnu Priya S
Vishnu Priya S

Posted on

JS variables , Functions &Objects

Variables

  1. let-- a keyword used to declare re-assignable, block-scoped local variables.
  2. var-- re- declaring the values.
  3. const-- you can't change the values.

Complier -- debug your whole program and translate to the machine

Intrepreter -- line by line debug and translates them to the machine.

Function

The function name should starts with lowercase, declaring the function and calling it (invoking) a reusable block of code designed to perform a specific task.

eg: function subj(tam, eng, math, sci, ss){
let add= (tam+ eng+ math+ sci+ ss);
console.log(add);
result add;
}
subj(80,50,90,40,54);

here the function name is subj and it has a arguments of tam, eng..
it stores the values into the variable called add. again calling the function to execute the code.

return

return is a keyword it returns the value not the variable
eg:The return keyword passes back the value (314), not the variable name (add).

objects

objects used to group and store all the variable with the help of obj name, in obj you can create another obj

const laptop={
brand: "hp",
price: "20000",
camera:{
back:50;
front:20;
}
}

console.log(laptop.camera.back) // o/p: 50

here the laptop is obj name and i'm calling the other obj name with the help of (.) and calling the var and that fetches the actual value.

constructor function

  1. when the obj is created the constructor called automatically.
  2. constructor name should starts with uppercase(Student)
  3. used to initialize object specific value.

const laptop1 = new Laptop("hp", 20000);

here new is a keyowrd storing the values into Laptop.

function Laptop(i, j){
this.brand= i;
this.price=j;

const laptop1 = new Laptop("hp", 20000);

console.log(laptop1.brand); // o/p:hp

the line new Laptop("hp", 20000) is the act of calling the function.

1.A Normal Function: You call it using just its name and parentheses:
subj(80, 50, 90);
2.A Constructor Function:You call it by putting the word new right in front of it:
new Laptop("hp", 20000);

Top comments (0)