console.log('Hello, World');
const place = "World";
const greeting = "Hello";
console.log("%s, %s", greeting, place);
// 模板字符串,更易读
console.log(`${greeting}, ${place}`);
${}
包裹。// 单行注释
/*
多行
注释
*/
定义的变量在该函数内处处有效。
var one = 1
undefined
)console.log(hello);
var hello = "Hello";
// undefined
只能作用在块中(用大括号包裹)。
let two = 2
const three = 3
尽可能多用、优先考虑 const
;
在循环中尽量用 let
;
没必要用 var
。
通常做法:
let str1 = "JavaScript";
let str2 = "fun";
console.log(`Formating in ${str1} is ${str2}!`);
// Formating in JavaScript is fun!
可以用表达式:
let bool1 = true;
console.log(`1 + 1 is ${1 + 1}`);
// 1 + 1 is 2
console.log(`The opposite of true is ${!bool1}`);
// The opposite of true is false
可以写多行:
console.log(`The opposite
of true
is ${!bool1}`);
/*
The opposite
of true
is false
*/
根据前面字符的 Unicode 码的大小比较。
false
外的假值''
null
undefined
0
x & y
x && y
建议用后者。
x | y
x || y
建议用后者。
!x
从左往右,如果从第一个操作数就可以判定结果,则不会继续判定。只有当第一个操作数的值无法确定逻辑运算的结果时,才对第二个操作数进行求值。