Type Coercion in Javascript

samuelma

Samuel Majok

Posted on June 4, 2023

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
πŸ’– πŸ’ͺ πŸ™… 🚩
samuelma
Samuel Majok

Posted on June 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related