Posts

Showing posts from May, 2022

Data Type Coercion in js

Image
    What is Type Coercion in JavaScript ? Type Coercion refers to the process of automatic or implicit conversion of values from one data type to another. This includes conversion from Number to String, String to Number, Boolean to Number etc. when different types of operators are applied to the values. In case the behavior of the implicit conversion is not sure, the constructors of a data type can be used to convert any value to that datatype, like the Number() , String() or Boolean() constructor. 1. String to Number Conversion: When any string or non-string value is added to a string, it always converts the non-string value to a string implicitly. When the string ‘Rahul’ is added to the number 10 then JavaScript does not give an error. It converts the number 10 to string ’10’ using coercion and then concatenates both the strings. Some more examples are shown below.    

Hoisting

Image
            Hoisting   JavaScript hoists function declarations and variable declarations to the top of the current scope. Variable assignments are not hoisted ( JavaScript only hoists declarations, not initializations!). Declare functions and variables at the top of your scripts, so the syntax and behavior are consistent with each other.