DEV Community

Kavin Loyola S
Kavin Loyola S

Posted on • Edited on

JS Datatypes & Operators

Variable Declaration

In JavaScript declaring variables is different from other Programming Languages, for example:

i = 10;
i = 20;
console.log(i);

Enter fullscreen mode Exit fullscreen mode

but this allows duplicate variables.

var i =10;
i = 20;
console.log(i);

Enter fullscreen mode Exit fullscreen mode

this also allows duplicate variables.

let i = 10;
let i = 20;
console.log(i);

Enter fullscreen mode Exit fullscreen mode

this is the method we use for variable declaration. because it doesn't allows duplicates.

const i = 10;

Enter fullscreen mode Exit fullscreen mode

this is used for constant value and doesn't changeable.

<script> - inside this tag we write JavaScript. and it will only run
in console and not on webpage/UI.

console.log( ) is used for printing Statements.

Operators

+

10 + 5 

o/p - 15

Enter fullscreen mode Exit fullscreen mode
"leo" + 2

o/p - leo2

Enter fullscreen mode Exit fullscreen mode

if we add (+) string to any values it also acts as string.

-

10 - 2

o/p - 8

Enter fullscreen mode Exit fullscreen mode
10 - "2" 

o/p - 8

Enter fullscreen mode Exit fullscreen mode

the above is the example for type casting, here the string converts into int and - .

*

10 * 2

o/p - 20

Enter fullscreen mode Exit fullscreen mode
"5" * 5

o/p - 25

Enter fullscreen mode Exit fullscreen mode

here is the another example for type casting.

/

10 / 2

o/p - 5

Enter fullscreen mode Exit fullscreen mode
10 / "2"

o/p - 5

Enter fullscreen mode Exit fullscreen mode

It takes Quotient as answer.

%

10 % 3

o/p - 1

Enter fullscreen mode Exit fullscreen mode

it takes remainder as answer.

Top comments (0)