DEV Community

Samuel Majok
Samuel Majok

Posted on

Type Coercion in Javascript

Interesting fact...

I was asked about this type coercion in an interview and I said that question was irrelevent since it had no use in my day today operations and the interviewer literally asked me to just define it and I was left fidgetting....

Yes, you need to have a very strong grasp with the basics of javascript apparently.
Infact, after diving deep into the topic of type coercion, I learnt so much that I didn't know and how important it was...

  • - - - - - > #hashtag LEARN YOUR BASICS

TYPE COERCION....
Definition: This is the automatic or implicit conversion of values from one type to another

Example....
Enter fullscreen mode Exit fullscreen mode
        let a = 1;
        let b = "5";

        let c = a + b;
        console.log(c);
        // the value of c will be 15
        // this is because addition converts numbers to string

        let d = 5 - 1;
        console.log(d);
        // the value of d will be 4
        // this is because subtraction converts strings to numbers
Enter fullscreen mode Exit fullscreen mode

Top comments (0)