Data types conversions in javascript

Data types conversions in javascript

Number conversions

  1. String to Number conversion

    When we want to convert a String to a number we use Number() function

     let goalsScored="4";
     let goalsScoredInNum= Number(goalsScored)  //Output: 4
     //if we check type of goalsScoredInNum it will show as Number
     console.log(typeof goalsScoredInNum);
    
  2. Invalid String to Number conversion

    When we try to convert the string which cannot be converted into a number the converted value will be NaN i.e, not a number

     let scoreSheet = "43 Messi"
     let scoreSheetInNum= Number(scoreSheet )  //Output: NaN
     //if we check type of scoreSheetInNum it will show as Number
     console.log(typeof goalsScoredInNum);
    
  3. Conversion of Null to Number

    When we try to convert null to a number the converted value turns out to be zero

     let nullValue= null //this value is null and cannot be converted to number
    
     console.log(typeof nullValue); //when we check type it shows Object
    
     let ConvertedValue= Number(nullValue) //we try to convert it into number
    
     console.log(typeof ConvertedValue); //when we check type of converted value it shows type as number
    
     console.log(ConvertedValue); //Output: 0 when we console log the actual value it shows as 0 so null converted to number is 0
    
  4. Conversion of undefined variable to Number

    Direct conversion of any undefined number to a number, will result in NaN however when we check the type of converted variable it shows as Number

     let undefinedVal; //undefined variable
     let convertedUndefinedVal = Number(undefinedVal)
     console.log(convertedUndefinedVal); //Output: NaN
     console.log(typeof convertedUndefinedVal); //Output: Number
    
  5. Conversion of Boolean to Number

    When we try to convert a boolean to a Number then true value will result in 1 and false value will result in 0

     let booleanVariable = true; //Boolean variable true
     let convertedBooleanVal = Number(booleanVariable)
     console.log(convertedBooleanVal); //Output: 1
     console.log(typeof convertedBooleanVal); //Output: Number
    
     let booleanVariable2 = false; //Boolean variable false
     let convertedBooleanVal2 = Number(booleanVariable2)
     console.log(convertedBooleanVal2); //Output: 0
     console.log(typeof convertedBooleanVal2); //Output: Number
    

Boolean Conversions

  1. String to Boolean

    When we try to convert String to boolean for any string the converted output will be true except for the empty string for empty string the converted output will be false as shown below

     //converting string to boolean
     let StringVal = "Uday" //String 
     let boolVal = Boolean(StringVal); //converting a String to boolean
     console.log(typeof boolVal); //type of converted value it shows boolean
     console.log(boolVal); //Output: true
     let StringVal2 = "" //emptyString
     let boolVal2 = Boolean(StringVal2); // converting an empty string to boolean
     console.log(typeof boolVal2); //type of converted value it shows boolean
     console.log(boolVal2); //Output: false
    
  2. Number to Boolean

    When we try to convert a number to boolean it convert it to true for any number except for the zero for zero only the converted output will be false as shown below

     //converting +ve Number to boolean
     let NumberVal1 = 4; // +ve number value 
     let BoolNumVal1 = Boolean(NumberVal1); // converting number to boolean
     console.log(typeof BoolNumVal1); //shows type as boolean
     console.log(BoolNumVal1); //Output: true 
    
     //converting 0 to boolean
     let NumberVal2 = 0; // 
     let BoolNumVal2 = Boolean(NumberVal2); // converting number to boolean
     console.log(typeof BoolNumVal2); //shows type as boolean
     console.log(BoolNumVal2); //Output: false
    
     //converting -ve Number to boolean
     let NumberVal3 = -3; // 
     let BoolNumVal3 = Boolean(NumberVal3); // converting number to boolean
     console.log(typeof BoolNumVal3); //shows type as boolean
     console.log(BoolNumVal3); //Output: true
    
  3. Null to Boolean

    When we try to convert the Null value to a boolean the converted value will be false

     let nullValue = null;
     let boolValue = Boolean(nullValue); 
     console.log(boolValue) //Output: false
     console.log(typeof boolValue)//Output: Boolean
    
  4. undefined to boolean

    When we try to convert the undefined value to a boolean the converted value will be false

     let undefinedValue = undefined;
     let boolValue = Boolean(undefinedValue); 
     console.log(boolValue) //Output: false
     console.log(typeof boolValue)//Output: Boolean
    

String Conversions

  1. Number to String Using toString() method

    We can convert Number to string using the toString method as shown below

     let score = 23;
     //converting the score to String using toString method
     let scoreInString = score .toString();
     console.log(scoreInString); // Output: "23"
     //if we check typeOf scoreInString it will give output as String
     console.log(typeof scoreInString)
    
  2. Number to String Using String function

    We can convert Number to string using the String method as shown below

     let score = 23;
     //converting the score to String using toString method
     let scoreInString = String(score);
     console.log(scoreInString); // Output: "23"
     //if we check typeOf scoreInString it will give output as String
     console.log(typeof scoreInString)
    
  3. Boolean to String

    When we convert boolean to String for boolean true the converted String would be "true" and for boolean false the converted String would be "false" as shown below

     let isTrue=true //bool value true
     let StringVal= String(isTrue); //converting it to string
     console.log(typeof StringVal); // printing type of StringVal: string
     console.log(StringVal); //printing value of StringVal: "true" 
    
     let isFalse=false //bool value false
     let StringVal2= String(isFalse); //converting it to string
     console.log(typeof StringVal2); // printing type of StringVal2: string
     console.log(StringVal2); //printing value of StringVal2: "false"
    
  4. Undefined to String

    When we try to convert undefined to String the converted value would be "undefined" and the type of the converted value will be String

     let undefinedValue = undefined;
     console.log(undefinedValue);
     let stringValue = String(undefinedValue); 
     console.log(stringValue);//Output: undefined
     console.log(typeof stringValue);//Output String
    
  5. Null to String

    When we try to convert null to String the converted value would be "null" and the type of the converted value will be String

     let nullValue = null;
     console.log(nullValue);
     let stringValue = String(nullValue); 
     console.log(stringValue);//Output: undefined
     console.log(typeof stringValue);//Output String