Tutorialspoint.dev

JavaScript | boolean.valueOf() function

The Boolean.valueOf() is an inbuilt function in javascript which is used to return a boolean value either “true” or “false” depending upon the value of the specified boolean object.
It returns false if the string argument is null otherwise it returns true.

Syntax:

boolean.valueOf()

Parameter:
Here nothing as a parameter is passed.

    Return Values:
    It returns a boolean value either “true” or “false” depending upon the value of the specified boolean object.

Let’s see some JavaScript code on this boolean.toString() function:



  • Code #1:
    // Here Boolean object obj is created 
    // for the value true.
    var obj = new Boolean(true);
      
    // Here boolean.valueOf() function is 
    // used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"
  • Code #2:
    // Here Boolean object obj is
    // created for the value 1.
    var obj = new Boolean(1);
      
    // Here boolean.valueOf() function 
    // is used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"
  • Code #3:
    // Here Boolean object obj is 
    // created for the value -1.
    var obj = new Boolean(-1);
      
    // Here boolean.valueOf() function
    // is used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"
  • Code #4:
    // Here Boolean object obj is 
    // created for the value 1.2
    var obj = new Boolean(1.2);
      
    // Here boolean.valueOf() function 
    // is used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"
  • Code #5:
    // Here Boolean object obj is 
    // created for the value as string "gfg"
    var obj = new Boolean("gfg");
      
    // Here boolean.valueOf() function is 
    // used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"
  • Code #6:
    // Here Boolean object obj is created for the value false.
    var obj = new Boolean(false);
      
    // Here boolean.valueOf() function is
    // used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "false"
  • Code #7:
    // Here Boolean object obj is created 
    // for the value zero (0)
    var obj = new Boolean(0);
      
    // Here boolean.valueOf() function is 
    // used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "false"
    1. Errors and Exceptions:

    2. Here the value as geeksforgeeks gives error because this value is not defined only true and false has been predefined.
      // Here Boolean object obj is created 
      // for the value geeksforgeeks.
      var obj = new Boolean(geeksforgeeks);
        
      // Here boolean.valueOf() function is
      // used for the created object obj.
      console.log(obj.valueOf());

      Output:

      Error: geeksforgeeks is not defined
    3. Here complex number can not be taken as the parameter only integer values and string can be taken as the parameter that is why it returns error.
      // Here Boolean object obj is created 
      // for the value such as complex number 1+2i
      var obj = new Boolean(1 + 2i);
        
      // Here boolean.valueOf() function is
      // used for the created object obj.
      console.log(obj.valueOf());

      Output:

      Error: Invalid or unexpected token

    Application:
    This function’s application is to return a boolean value of either “true” or “false” depending upon the value of the object given.
    Let’s see JavaScript program on this application:

    // Here Boolean object obj 
    // is created for the value 27
    var obj = new Boolean(27);
      
    // Here boolean.valueOf() function is 
    // used for the created object obj.
    console.log(obj.valueOf());

    Output:

    > "true"


    This article is attributed to GeeksforGeeks.org

    leave a comment

    code

    0 Comments

    load comments

    Subscribe to Our Newsletter