DEV Community

R.Shobika CSE
R.Shobika CSE

Posted on • Edited on

Variables

TYPES OF VARIABLES:
In Javascript there are 4 types of variables

  1. Automatically: Undeclared variables are automatically declared variable. It is not basically recommended because we can reinitialize but we can't see the different like where we declare and where we reinitialize for.eg., x=5; y=6; res=x+y;
  2. var: var variables are also similar to the automatically variables same like that we cant see the different between the declare and reinitialize for.eg., var x=5; var y=6; var res=x+y;
  3. let: let variable are used in JavaScript commonly. In this type we can reinitialize and also we see the difference . when we reinitialize should not give again the variable type . We only give variable name like for.eg., let x=7; x=8;

4.const:
const variable is used in JavaScript. In this type we cannot reinitialize because const variable is a constant type we can't change that
for.eg.,
const y=8;

HOW TO WRITE A JS?

 In HTML code in <body> tag we have to give <script> tag like internal CSS. In CSS we give <style> same like that in JS we have to give <script>. In that script tag we have to write the code .
Enter fullscreen mode Exit fullscreen mode
html
<body>
<script>
 let x=8
 let y=9
 let res=x+y
</script>
</body>
Enter fullscreen mode Exit fullscreen mode

OPERATIONS:

1.ARITHMETICS OPERATION:

   Arithmetic operation is the normal mathematical calculation .In this operations there are various types [+,-,*,/,%]
Enter fullscreen mode Exit fullscreen mode

(i)Addition:

let x=5
let y=7
console.log(x+y)
Enter fullscreen mode Exit fullscreen mode

Output:12

let x=5
let y="7"
console.log(x+y)
Enter fullscreen mode Exit fullscreen mode

Output:57
(This method is called concatenation method. In + there are two method one is normal add . if we give two number means it do a addition calculation . if we give one number and one string means it do a concatenation . concatenation means it will combines the values).

(ii) Subraction:

let x=5
let y=8
console.log(x-y)
Enter fullscreen mode Exit fullscreen mode

output:3

let x=5
let y="8"
console.log(x-y)
Enter fullscreen mode Exit fullscreen mode

output:3

(This method is not a concatenation . In addition only it will concatenate if otherwise string will consider as a number )

(iii) Multiplication:

let x=5
let y=8
console.log(x*y)
Enter fullscreen mode Exit fullscreen mode

output:40

let x=5
let y="8"
console.log(x*y)
Enter fullscreen mode Exit fullscreen mode

output:40

( This method is not a concatenation . In addition only it will concatenate if otherwise string will consider as a number ).

** (iv)Division:**

  Normal mathematical division but it does not take a remainder it will take quotient value
Enter fullscreen mode Exit fullscreen mode
let x=4
let y=2
console.log(x/y)
Enter fullscreen mode Exit fullscreen mode

output:2

let x=4
let y="2"
console.log(x/y)
Enter fullscreen mode Exit fullscreen mode

output:2

(This method is not a concatenation . In addition only it will concatenate if otherwise string will consider as a number ).

(v).Modulus:

 In this operation it also like a division but it will take the remainder answer
Enter fullscreen mode Exit fullscreen mode
let x=4
let y=2
console.log(x%y)
Enter fullscreen mode Exit fullscreen mode

output:0

let x=4
let y="2"
console.log(x%y)
Enter fullscreen mode Exit fullscreen mode

output:0

( This method is not a concatenation . In addition only it will concatenate if otherwise string will consider as a number ).

2. INCREMENT/DECREMENT OPERATORS:

In this operator there are 2 types
1. Post Increment/decrement (i++)
2. Pre Increment /decrement (++i)
Enter fullscreen mode Exit fullscreen mode

(i). Post Increment/decrement:

  In this increment/decrement the increment/decrement value store in the memory then if we give the next line print statement means it will show the increment/decrement value
Enter fullscreen mode Exit fullscreen mode
let i=9;
 i++
Enter fullscreen mode Exit fullscreen mode

output:9

The output will be the 9 not 10 because it is a post increment/decrement so what the output value is 9 the 10 will be store in the memory.

(ii). Pre Increment/decrement:

  In this increment / decrement the increment/decrement value show in the output .
Enter fullscreen mode Exit fullscreen mode
 let i=9;
    ++i;
Enter fullscreen mode Exit fullscreen mode

output:10

the output will be the 10 not 9 because it is pre increment/decrement so what the output value is 10 not 9.

Top comments (0)