CodeGym /Java Course /Frontend SELF EN /Type Conversion in JavaScript

Type Conversion in JavaScript

Frontend SELF EN
Level 35 , Lesson 0
Available

1.1 Explicit Type Conversion

Type conversion in JavaScript is the process of converting a value from one data type to another. There are two types of conversion: explicit (manual) and implicit (automatic). Understanding these processes is important to prevent errors and create predictable code.

Explicit conversion, also known as manual conversion, is performed using built-in JavaScript functions and methods. This is the conversion where the programmer explicitly specifies which data type should be converted to another type.

Main methods of explicit conversion:

1. Conversion to string

  • Method String(value): converts a value to a string
JavaScript
    
      let num = 123;
      let str = String(num);
      console.log(typeof str); // "string"
    
  

  • value.toString(): object method that converts a value to a string
JavaScript
    
      let bool = true;
      let str = bool.toString();
      console.log(typeof str); // "string"
    
  

2. Conversion to number

  • Number(value): converts a value to a number
JavaScript
    
      let str = "456";
      let num = Number(str);
      console.log(typeof num); // "number"
    
  

  • parseInt(value, base): converts a string to an integer. The base parameter indicates the base of the numeral system
JavaScript
    
      let str = "123";
      let num = parseInt(str, 10);
      console.log(typeof num); // "number"
    
  

  • parseFloat(value): converts a string to a floating-point number
JavaScript
    
      let str = "123.45";
      let num = parseFloat(str);
      console.log(typeof num); // "number"
    
  

3. Conversion to boolean

  • Boolean(value): converts a value to boolean
JavaScript
    
      let str = "";
      let bool = Boolean(str);
      console.log(typeof bool); // "boolean"
    
  

1.2 Implicit Type Conversion

Implicit conversion, also known as automatic or hidden conversion, is performed by JavaScript automatically when performing operations with different data types. Implicit conversion can lead to unexpected results, so it needs to be understood and used with caution.

1. Conversion to string

Important!

Whenever you add any object to a string, JavaScript will attempt to convert it to a string.

Example:

JavaScript
    
      let result = "The number is " + 123;
      console.log(result); // "The number is 123"
    
  

2. Conversion to number

Important!

Whenever you use any arithmetic operators (except +) with strings containing numbers.

JavaScript
    
      let result = "123" - 0;
      console.log(result); // 123 (number)
    
  

How this works in practice:

JavaScript
    
      let result = "123" - 10;
      console.log(result); // 113 (number)

      let result2 = "123" + 10;
      console.log(result2); // 12310 (string)
    
  

3. Conversion to boolean

When using values in logical contexts (conditional operators, loops, etc.).

JavaScript
    
      let value = "hello";

      if (value) {
        console.log("Value is truthy");   // Output: "Value is truthy"
      }
    
  

Examples of conversions in a logical context:

  • false, 0, "" (empty string), null, undefined, and NaN are considered false values
  • All other values are considered true
JavaScript
    
      if ("") {
        console.log("This won't be logged.");
      } else {
        console.log('"" is considered false in a boolean context.');
      }

      // '"" is considered false in a boolean context.'
    
  

1.3 Examples of Implicit Conversion

Example with arithmetic operations:

JavaScript
    
      let width = "100";
      let height = "200";

      let area = Number(width) * Number(height); // Explicit conversion
      console.log(area); // 20000

      let perimeter = (+width) + (+height); // Using unary plus for explicit conversion
      console.log(perimeter); // 300
    
  

Example with logical context:

JavaScript
    
      let userInput = ""; // Empty string

      if (!userInput) {
        console.log("User did not enter data."); // Implicit conversion of string to boolean
      }
    
  

Example with combined types:

JavaScript
    
      let value = "10";
      let increment = 5;

      let result = value + increment; // Implicit conversion of number to string
      console.log(result); // "105"

      result = Number(value) + increment; // Explicit conversion of string to number
      console.log(result); // 15
    
  
1
Task
Frontend SELF EN, level 35, lesson 0
Locked
String Conversion
String Conversion
1
Task
Frontend SELF EN, level 35, lesson 0
Locked
Implicit Addition
Implicit Addition
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION