Is it possible to change a variable from String to Number in Javascript?

Bernard Mizzi

I would like to set a variable that will store the return value of functions. The return value can either be a string, number or boolean but I don't have any guarantee which one of them will be returned.

Is it possible to use the variable to store either a number, string or boolean depending on the type that the functions will return without applying any conversions?

For example, first I declare var returnValue = 0. Then a function functionAmay return the string 'passed' and I want to store it in returnValue by writing returnValue = functionA(). Then another function functionBmay return 10 or false and I would like to use returnValue by writing returnValue = functionB(). Can it be done?

ivp

Yes you can.

In Javascript, a 'var' can store any type of data as JS is untyped as referred in the comments in the main question.

function fn(type) {
  if(type == 'string') return 'passed';
  if(type == 'number') return 1;
  if(type == 'boolean') return true;
}

let returnedValue;
returnedValue = fn('string');
console.log(returnedValue, typeof returnedValue);

returnedValue = fn('number');
console.log(returnedValue, typeof returnedValue);

returnedValue = fn('boolean');
console.log(returnedValue, typeof returnedValue);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Extract a number from a string (JavaScript)

From Java

Check whether variable is number or string in JavaScript

From Dev

Mixing Razor and Javascript: Assigning a number from Model to a Javascript variable in a view

From Dev

javascript: push array values from string variable

From Dev

jquery/javascript declare variable from string

From Dev

Extract a number from a string? (Javascript)

From Dev

is it possible to change JS variable value from browser console?

From Dev

Escaping double quotes in JavaScript from a string in a variable

From Dev

Converting from number to string gives strange number in Javascript

From Dev

Boolean variable returns as string from javascript function

From Dev

Pandas - change string to a number

From Dev

is it possible to get ieee: float "bits" from javascript number?

From Dev

Is it possible to instantiate a javascript class from a dynamic variable?

From Dev

Is it possible to change number systems in python?

From Dev

is it possible to divide JavaScript variable?

From Dev

Javascript random number and background change from button

From Dev

Extract a number from a string? (Javascript)

From Dev

is it possible to change JS variable value from browser console?

From Dev

Passing a string from .NET to a javascript variable

From Dev

How to count number of lines in javascript html string variable

From Dev

Variable name from string and number in CMD FOR loop?

From Dev

Is it possible to get a variable from a string in batch?

From Dev

Is it possible to change the value of environment variable from a bash script so that it persists?

From Dev

javascript regex - extract number from string

From Dev

How to change JavaScript variable from PHP?

From Dev

Javascript getting number from string

From Dev

Get variable contents from string using javascript

From Dev

Is it possible to sent a string variable into a JavaScript?

From Dev

javascript increment from number to string

Related Related

  1. 1

    Extract a number from a string (JavaScript)

  2. 2

    Check whether variable is number or string in JavaScript

  3. 3

    Mixing Razor and Javascript: Assigning a number from Model to a Javascript variable in a view

  4. 4

    javascript: push array values from string variable

  5. 5

    jquery/javascript declare variable from string

  6. 6

    Extract a number from a string? (Javascript)

  7. 7

    is it possible to change JS variable value from browser console?

  8. 8

    Escaping double quotes in JavaScript from a string in a variable

  9. 9

    Converting from number to string gives strange number in Javascript

  10. 10

    Boolean variable returns as string from javascript function

  11. 11

    Pandas - change string to a number

  12. 12

    is it possible to get ieee: float "bits" from javascript number?

  13. 13

    Is it possible to instantiate a javascript class from a dynamic variable?

  14. 14

    Is it possible to change number systems in python?

  15. 15

    is it possible to divide JavaScript variable?

  16. 16

    Javascript random number and background change from button

  17. 17

    Extract a number from a string? (Javascript)

  18. 18

    is it possible to change JS variable value from browser console?

  19. 19

    Passing a string from .NET to a javascript variable

  20. 20

    How to count number of lines in javascript html string variable

  21. 21

    Variable name from string and number in CMD FOR loop?

  22. 22

    Is it possible to get a variable from a string in batch?

  23. 23

    Is it possible to change the value of environment variable from a bash script so that it persists?

  24. 24

    javascript regex - extract number from string

  25. 25

    How to change JavaScript variable from PHP?

  26. 26

    Javascript getting number from string

  27. 27

    Get variable contents from string using javascript

  28. 28

    Is it possible to sent a string variable into a JavaScript?

  29. 29

    javascript increment from number to string

HotTag

Archive