DEV Community

boopalan
boopalan

Posted on

implicit and explicit type conversion and Arithmetic operations.

implicit type conversion

for doing operation in between the type need same types. But in real world programming we cannot always expect that we will be performed in between the same types. In those cases we have to handle different types, JavaScript has some in-built behaviours to handle different types, that is called implicit type conversion.

It will automatically convert the other type to a suitable type for doing operation whenever needed.

For example if we do an addition operation in between the string and the number.

hi + 4
Enter fullscreen mode Exit fullscreen mode

it was converted that another type to string

is it similar if we do

hi + 4
Enter fullscreen mode Exit fullscreen mode

in another case suppose if we do subtraction in between the two type what will happen

10 - 30
Enter fullscreen mode Exit fullscreen mode

this time we will get different answer because instead of converting one type to another type it convert the string into to Number type(we cannot be perform subtraction between the string types, so it convert the string to Number and we finally get the -20)

explicit type conversion:

What is the difference between Implicit and Explicit Type Conversion?

implicit: Done automatically by the compiler. It safely promotes smaller types to larger types (e.g., short to int or int to double) to avoid data loss.
Enter fullscreen mode Exit fullscreen mode

Explicit (Casting): Manually enforced by the developer using a cast operator (e.g., (int)myDouble). It is used to convert larger types to smaller types, carrying a risk of truncation or data loss. [1, 2, 3, 4, 5]

TBD

  • what will happen If we need to convert the Non-Premitive types to non primitive type case

  • can we be able to do operations between the non-primitive types.

subtraction operator

just like the name it is used to perform the operation between the Number types of element
Enter fullscreen mode Exit fullscreen mode

examples (trying this in the browsers console or any other JavaScript runtime environments in the machine )

10 -5 // 5
50-50 //0
Enter fullscreen mode Exit fullscreen mode

**
multiplication operator**

if we perform operation between the Number it will multiplay, But if we perform the operation between the String and the number it will return NaN(not a number)
Enter fullscreen mode Exit fullscreen mode
10 * 10 // 100
10 * 10 // NaN
Enter fullscreen mode Exit fullscreen mode

division operator
As well as division is one fundamental operation in the maths and it will helps for us in several area

10/1 // 10
5/5 // 1
Enter fullscreen mode Exit fullscreen mode

In this example it gives us Number type, suppose if we perform an operation that gives us irrational numbers, then how it will be handled by the JavaScript.

Example

it is runned inside the nodejs

> a = 10
10
> b = 7
7
> c = 10/7
1.4285714285714286
> typeof(c)
'number'
>

Enter fullscreen mode Exit fullscreen mode

when you notice the type of the c, it value is irrational but if we see the type it is actually a number,

In many other programming languages the float or irrational number is considered as the different data type but in JavaScript it is considered as the Number.

modules operator
to check the remainder between two number will help many scenarios, For example to find the odd or even


> 11%2
1
> 11%3
2
> 11%11
0
> 11%1
0
> 23%3
2
Enter fullscreen mode Exit fullscreen mode

exponential operator
it is used to find the square of a math number.

> 2**3
8
> 2**1
2
> 2**0
1
> 5**10
9765625
>2**3
8
Enter fullscreen mode Exit fullscreen mode

increment operator
most case we will use the increment operation in the for loop and while loop, here we will see that how it was working

let I = 1;
I = I + 1
console.log(I) // 2

let I = 1;
I ++
console.log(I) // 2

Enter fullscreen mode Exit fullscreen mode

decrement operator
Just like the increment we will use the decrement operator, it also works like the increment but instead of increasing count it reduces the count.

let I = 1;
I = I - 1
console.log(I) // 0

let I = 1;
I --
console.log(I) // 0

Enter fullscreen mode Exit fullscreen mode
post increment and decrement operator
Enter fullscreen mode Exit fullscreen mode
It see the increase operation deeply it was actually first use the values in stored in the variable and later it is increase the value that’s why it is called post increment
Enter fullscreen mode Exit fullscreen mode

post increment operator


let I = 1;
console.log(I++) // 1
console.loglet


I = 1;
I ++;
console.log(I++) // 2(I) // 2


Enter fullscreen mode Exit fullscreen mode

post decrement operator

let I = 1;
console.log(I--) // 1
console.log(I) // 2


let I = 1;
I --;
console.log(I--) // 2

Enter fullscreen mode Exit fullscreen mode

**
pre increment and pre decrement operator**

suppose if do now want to use the values first and then increase the value in the variable, you want to first increment or decrement the value first and then you want to use the values in the program you also do that with the help of the pre increment and pre decrement operators
Enter fullscreen mode Exit fullscreen mode

pre increment operator

let I = 1;
console.log(++I) // 1
console.log(I) //

I = 1;
++I;
console.log(I++) // 2(I) // 2

Enter fullscreen mode Exit fullscreen mode

pre decrement operator

let I = 1;
console.log(--I) // 1
console.log(I) // 2


let I = 1;
--I;
console.log(I) // 2

Enter fullscreen mode Exit fullscreen mode

Question of the question based on our learnings

let I =5;
let j = 7;
result = i++ - --j + ++j i++;
log(i,j,result)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)