Javascript
运算
- 赋值运算符
- 顾名思义(简单的例子:将a赋给b)
例子:
var x = 2;
var y = 3;
console.log(x);
// expected output: 2
console.log(x = y + 1); // 3 + 1
// expected output: 4
console.log(x = x * y); // 4 * 3
// expected output: 12
赋值运算结果。
名称 | 简单形式 | 含义 |
---|---|---|
赋值(Assignment) | x = y | x=y |
加赋值(Addition assignment) | x += y | x = x + y |
减赋值(Subtraction assignment) | x -= y | x = x - y |
乘赋值(Multiplication assigment) | x *= y | x = x * y |
除赋值(Division assignment) | x /= y | x = x / y |
模赋值(Remainder assignment) | x %= y | x = x % y |
指数赋值(Exponentiation assignment) | x **= y | x = x ** y |
左移赋值(Left shift assignment) | x <<= y | x = x << y |
右移赋值(Right shift assignment) | x >>= y | x = x >> y |
无符号右移赋值(Unsigned right shift assignment) | x >>>= y | x = x >>> y |
按位与赋值(Bitwise AND assignment) | x &= y | x = x & y |
按位异或赋值(Bitwise XOR assignment) | x ^= y | x = x ^ y |
基本的赋值运算符是等号(=),该运算符把它右边的运算值赋给左边。即,x = y 把 y 的值赋给 x。 其他的赋值运算符通常是标准运算符的简写形式,如下面的定义与示例。
逗号操作符 对它的每个操作数求值(从左到右),并返回最后一个操作数的值。
eg:
var x = 1;
x = (x++, x);
console.log(x);
// expected output: 2
x = (2, 3);
console.log(x);
// expected output: 3
类型转换
JavaScript 类型转换
Number() 转换为数字, String() 转换为字符串, Boolean() 转化为布尔值。
JavaScript 数据类型
在 JavaScript 中有 5 种不同的数据类型:
string
number
boolean
object
function
3 种对象类型:
Object
Date
Array
2 个不包含任何值的数据类型:
null
undefined
你可以使用 typeof 操作符来查看 JavaScript 变量的数据类型。
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<p> typeof 操作符返回变量、对象、函数、表达式的类型。</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof NaN + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34} + "<br>" +
typeof new Date() + "<br>" +
typeof function () {} + "<br>" +
typeof myCar + "<br>" +
typeof null;
</script>
</body>
</html>
这段代码输出的是:、
typeof 操作符返回变量、对象、函数、表达式的类型。
string
number
number
boolean
object
object
object
function
undefined
object
constructor 属性
constructor 属性返回所有 JavaScript 变量的构造函数。
eg:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<p> constructor 属性返回变量或对象的构造函数。</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"john".constructor + "<br>" +
(3.14).constructor + "<br>" +
false.constructor + "<br>" +
[1,2,3,4].constructor + "<br>" +
{name:'john', age:34}.constructor + "<br>" +
new Date().constructor + "<br>" +
function () {}.constructor;
</script>
</body>
</html>
输出为:
constructor 属性返回变量或对象的构造函数。
function String() { [native code] }
function Number() { [native code] }
function Boolean() { [native code] }
function Array() { [native code] }
function Object() { [native code] }
function Date() { [native code] }
function Function() { [native code] }
将数字转换为字符串
全局方法 String() 可以将数字转换为字符串。
该方法可用于任何类型的数字,字母,变量,表达式:
eg:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<p> String() 方法可以将数字转换为字符串。</p>
<p id="demo"></p>
<script>
var x = 123;
document.getElementById("demo").innerHTML =
String(x) + "<br>" +
String(123) + "<br>" +
String(100 + 23);
</script>
</body>
</html>
输出为:
String() 方法可以将数字转换为字符串。
123
123
123
Number 方法 toString() 也是有同样的效果。