String function

The String function converts a passed value to a string. Conversions for primitives (numbers, strings, and other simple types) are done in the most obvious way. For example: true - 'true', 5 - '5'. And objects are most often converted to '[object Object]', but there may be exceptions.

Syntax

javascriptString(what to convert);

Example

Let's convert the number 0 to a string:

javascriptString(0);

The code execution result:

javascript'0'

Example

And now let's set the non-numeric NaN value as the function parameter:

javascriptString(NaN);

As a result, we will get the string:

javascript'NaN'

Example

Let's convert the boolean false value:

javascriptString(false);

The code execution result:

javascript'false'

Example

Let's convert a string using the String function:

javascriptString('str');

After executing the code, we will get the same string:

javascript'str'

Example

Now let's set the function parameter to null:

javascriptString(null);

As a result of executing the code, we will get a value converted to the string:

javascript'null'

Example

Let's set the function parameter to undefined:

javascriptString(undefined);

The code execution result:

javascript'undefined'

Example

Now let's enter curly braces in the parameter as a designation of an empty object:

javascriptString({});

After executing the code, we will get the string that will specify the type of the given object:

javascript'[object Object]'

Example

And now we convert square brackets or an empty array:

javascriptString([]);

As a result, instead of specifying the object type, we get an empty string:

javascript''

Example

Let's add an array filled with numbers to the function parameter:

javascriptString([1, 2, 3]);

As a result of code execution, all array values are converted into one string:

javascript'1,2,3'

See also

  • the Number function
    that converts to a number
  • the Boolean function
    that converts to a boolean value