变量
var
var a = 1;
var s = "Yuoek";
let
let c = 1;
c = 5;
const
const PI = 3.14159;
区别
var:全局变量,不在 {} 内,可以先使用再声明定义
let: 局部变量,变量可以修改
const:静态变量,赋值后不可以修改
变量类型
数字
var num = 10;
字符串
var str = "String";
布尔
var b = true;
命名
var _name1 = "Name1";
var $name2 = "Name2";
输出
alert()
alert("Hello, Yuoek");
document.write()
document.write("Hello, Yuoek");
console.log()
console.log("Hello, Yuoek");
示例
<script>
console.log("Happy Everyday!")
var num1 = 10;
var num2 = 20;
console.log("The Sum is: " + num1 + num2)
var obj = {
id: "2557",
name: "Yuoek",
age: "25"
};
console.log(obj);
let message = "Do you best";
console.log(message);
</script>
innerHTML
<div id="Yu"></div>
<script>document.getElementById("Yu").innerHTML = "JavaScript Tutorial"</script>
注释
单行
// 这是注释
多行
/*
这也是注释
*/
作用域
var num = "这是全局变量";
function life() {
var num = "这是局部变量";
document.write(num);
}
let 同名变量在函数外不生效
<script>
let x = 10;
var y = 20;
function vl() {
let x = 100;
var y = 200;
document.write("x: " + x + "<br>" + "y: " + y);
}
vl();
</script>
修改变量
var a = 10;
var a = 100;
赋值,使用后再声明
a = 50;
document.write("a 是:" + a);
var a;
静态变量
const b;
b = 100;
// 只能赋值一次,下面报错
b = 200;
使用块作用域
{
const b = "Yuoek";
}
const b = "Sophie";
静态数组
const arr = ["Javascript", "c", "python"];
const[0] = "haskell";
arr.push("html");
document.write(arr);
静态对象
const cobj{
name: "Yuoek";
age: "18";
};
cobj.name = "Sophie";
cobj.age = "17";
document.write(JSON.stringify(cobj));
不先使用再声明
document.write(x);
const x = 10;
| Comparison basis | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Hoisted | Yes | No | No |
| Reassign | Yes | Yes | No |
| Re-declare | Yes | No | No |
| Bind This | Yes | No | No |
数据类型
string
var s1 = "这是字符串1";
var s2 = '这是字符串2';
var s3 = `这是字符串3`;
numbers
var n1 = 12e8;
var n2 = 20e-8;
boolean
var b = true;
var c = false;
null
var ty;
var name = "Name";
var name = null;
undefined
var ty;
var name = "Name";
var name = undefined;
BigInt
let BigNum = 1535837495935037423925305n;
Symbol
let syb1 = Symbol("520");
let syb2 = Symbol("520");
let same == syb1 == syb2
Object
let name = "Yuoek";
const obj = {
age: 18;
name: name;
};
Array
const arr = ["c", "cpp", "python", "java", "html", "css", "javascript"];
Date
let date = new date();
动态类型
let d = 'Yuoe';
d = 18;
d = true;
typeof
let name = "yuoek";
let age = 18;
document.write(typeof name + "<br>")
document.write(typeof age + "<br>")
类型转换
转字符串
document.write("10" + 20 + "<br>");
document.write("10" + true + "<br>");
document.write("10" + null + "<br>");
document.write("10" + undefined + "<br>");
// String()
document.write(typeof String(10))
// toString()
const num = 100;
num.toString()
转数字
document.write(("10" + 20) + "<br>");
document.write(("10" + true) + "<br>");
document.write(("10" + null) + "<br>");
document.write(("10" + undefined) + "<br>");
// num
document.write(Number('100'));
document.write(Number(null));
document.write(Number(false));
转布尔
num = !!0;
num = !! 1;
str = !!"";
str = !!"Hello";
document.write(Boolean("100"));
document.write(Boolean(null));
document.write(Boolean(undefined));
date 转 字符串和数字
// to Number
Number(date);
// or
date.getTime();
// to String
String(date);
// or
date.toString();
Value String conversion Number conversion Boolean conversion
0 “0” 0 false
1 “1” 1 true
“1” “1” 1 true
“0” “0” 0 true
"" "" 0 false
“Hello” “Hello” NaN true
true “true” 1 true
false “false” 0 false
null “null” 0 false
undefined “undefined” NaN false
[50] “50” 50 true
[50, 100] “[50, 100]” NaN true