Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS equivalent of Python str() method

In Python 2.7.3, there is a method called str() which turns all data types into strings. I was wondering if there was a string method like this in JavaScript and if so, what the syntax was for it.

like image 488
user1764199 Avatar asked Nov 26 '25 14:11

user1764199


1 Answers

string function in javascript

<script>

var test1 = new Boolean(1);
var test2 = new Boolean(0);
var test3 = new Boolean(true);
var test4 = new Boolean(false);
var test5 = new Date();
var test6 = new String("999 888");
var test7 = 12345;

document.write(String(test1)+ "<br />");
document.write(String(test2)+ "<br />");
document.write(String(test3)+ "<br />");
document.write(String(test4)+ "<br />");
document.write(String(test5)+ "<br />");
document.write(String(test6)+ "<br />");
document.write(String(test7)+ "<br />");

</script> 

The output of the code above will be:

true
false
true
false
Mon Oct 22 2012 16:30:10 GMT+0530 (India Standard Time)
999 888
12345

check this function too

var iNum1 = 10;
var fNum2 = 10.0;
var iNum = 10;

alert(iNum1.toString(2)); //outputs “1010”

alert(iNum1.toString(8)); //outputs “12”

alert(iNum1.toString(16)); //outputs “A”
like image 76
Ghostman Avatar answered Nov 29 '25 03:11

Ghostman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!