Home > Web Front-end > JS Tutorial > How to convert value to string type in javascript

How to convert value to string type in javascript

青灯夜游
Release: 2023-01-04 09:33:38
Original
4314 people have browsed it

Methods to convert values ​​into strings in javascript: 1. Use the toString() method, the syntax "variable.toString()"; 2. Use the String() function, which can convert the value of the object into String, syntax "string('value')"; 3. Use " " to splice a string.

How to convert value to string type in javascript

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

Convert to string type, there are three ways: Variable.toString(); , String('value');, use ' 'Splice a string;

Each method has a scope of use. We can draw the following conclusions through the following code:

In the basic data type:

( 1) Only number and boolean types call toString() for type conversion.

There is no toString() method in undefined and null.

toString() can only be used on variables, not constants.

(2) undefined and null can be converted into strings through String().

Constants can be converted to strings using String().

(3) All types can be converted into strings by concatenating strings.

In an array, you can call the toString() method and concatenate string conversion, but you cannot use String().

<script>
	// 转换成string类型,三种方式:变量.toString(); String(&#39;值&#39;); 使用&#39;+&#39;拼接一个字符串;
	
	// number类型转string
	
	// .toString()
	var num =123;
	num = num.toString();
	console.log(typeof num);//返回的是string;
	
	// String()
	var num2 =50;
	var str1 = String(num2);
	console.log(typeof str1);//返回string
	
	// 拼接字符串
	var num3 =60;
	var str1s = num3 +&#39;&#39;;
	console.log(str1s);
	console.log(typeof str1s);//返回string

	// 常量:
	
	// .toString()
	// 123.toString(); //编辑器报错,说明没有这种写法
	
	// String()
	var str1 = String(123);
	console.log(typeof str1);//返回string
	
	// boolean类型转string
	
	// .toString()
	var boo =false;
	boo = boo.toString();
	console.log(typeof boo);//返回的是string

	// String() 
	var boo2 =true;
    var str3 = String(boo2);
	console.log(typeof str3);//返回string
	
	// 拼接字符串
	var boo3 =false;
	var str3s = boo3 +&#39;&#39;;
	console.log(str3s);
	console.log(typeof str3s);//返回string
	
	// undefined转换成string
	
	// .toString()
	var und;
	und = und.toString();
	console.log(und);
	console.log(typeof und);
	//报异常: Cannot read property &#39;toString&#39; of undefined 

	// String() 
	var und2;
	var str4 = String(und2);
	console.log(typeof str4);//返回string

	// 拼接字符串
	var nud3;
	var str4s = nud3 +&#39;&#39;;
	console.log(str4s);
	console.log(typeof str4s);//返回string

	// null转换成string
	
	// .toString()
	var nul =null;
	nul = nul.toString();
	console.log(typeof nul);
	//报异常:Cannot read property &#39;toString&#39; of null

	// String() 
	var nul2 =null;
	var str5 = String(nul2);
	console.log(typeof str5);//返回string
	
	// 拼接字符串
	var nul3;
	var str5s = nul3 +&#39;&#39;;
	console.log(str5s);
	console.log(typeof str5s);//返回string

	// 复杂数据类型转字符串
	
	// 数组调用toString();
	var arr = [
		&#39;hello&#39;,
		&#39;world&#39;
	];
	arr = arr.toString();
	console.log(arr);//输出结果为“hello,world”;
	console.log(typeof arr);//返回的是string

	// 数组调用String();
	var ars = [
		&#39;jack&#39;,
		&#39;rose&#39;
	];
	ars = toString(ars);
	console.log(ars);//输出结果是[object Undefined]
	console.log(typeof ars);//返回的是string

	// 拼接字符串
	var arr = [
		&#39;hello&#39;,
		&#39;world&#39;
	];
	arr = arr +&#39;&#39;;
	console.log(arr);//输出结果为“hello,world”;
	console.log(typeof arr);//返回的是string
</script>
Copy after login

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to convert value to string type in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template