20 of 264 menu

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

String(what to convert);

Example

Let's convert the number 0 to a string:

String(0);

The code execution result:

'0'

Example

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

String(NaN);

As a result, we will get the string:

'NaN'

Example

Let's convert the boolean false value:

String(false);

The code execution result:

'false'

Example

Let's convert a string using the String function:

String('str');

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

'str'

Example

Now let's set the function parameter to null:

String(null);

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

'null'

Example

Let's set the function parameter to undefined:

String(undefined);

The code execution result:

'undefined'

Example

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

String({});

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

'[object Object]'

Example

And now we convert square brackets or an empty array:

String([]);

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

''

Example

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

String([1, 2, 3]);

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

'1,2,3'

See also

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