一.變量-聲明
// JS變量: 弱類型, 動(dòng)態(tài)類型
var a = 3
a = "lioil"
a = true
var b=2, c=3
// 加var聲明,作用范圍在代碼塊{}中
// 不加var聲明,作用范圍為全局
function hello(){
var h = "hello";
w = "world";
}
hello();
alert(h);
alert(w);
二.變量-原始類型primitive type(5種)
number 數(shù)字(不分整型或浮點(diǎn)型)
string 字符串
boolean 布爾
null 對(duì)象類型的占位符, 人為賦值null(系統(tǒng)不會(huì)自動(dòng)賦值null)
undefined 未定義, null衍生類型(變量沒(méi)有初始化,系統(tǒng)自動(dòng)賦值undefined)
// 多行的字符串直接量, 則需要在每行結(jié)束時(shí)加入反斜杠(\)
var str = "hello,\
world,\
nihao";
var a = 3;
var b = 3.14;
var c = "hello";
var d = 'world';
var e = true;
var f = null;
var g = undefined;
var h ;
var o = new Object();
// 運(yùn)算符typeof: 原始類型的類型判斷
alert(typeof a);
alert(typeof b);
alert(typeof c);
alert(typeof d);
alert(typeof e);
alert(typeof f); //typeof null結(jié)果是object, 這是個(gè)歷史遺留BUG, null是object的占位符,期望object
alert(typeof g);
alert(typeof h);
alert(typeof o);
console.log("hello") // 瀏覽器控制臺(tái)輸出(F12調(diào)試開(kāi)發(fā)者工具)
console.dir("world") // 瀏覽器控制臺(tái)顯示數(shù)據(jù)類型結(jié)構(gòu)
1.number數(shù)字
和其他語(yǔ)言不一樣,JavaScript不區(qū)分整數(shù)和浮點(diǎn)數(shù),JavaScript中的所有數(shù)字都是用浮點(diǎn)數(shù)來(lái)表示
JavaScript支持常見(jiàn)的算術(shù)表達(dá)式: 加(+) 減(-) 乘(*) 除(/) 求余(%)
JavaScript還支持更加復(fù)雜的運(yùn)算,被封裝在Math對(duì)象的方法中:
Math.pow(m,n) m的n次冪
Math.round(0.6) 四舍五入,結(jié)果為1
Math.ceil(0.6) 向上取整,結(jié)果為1
Math.floor(0.6) 向下取整,結(jié)果為0
Math.abs(-6) 取絕對(duì)值,結(jié)果為6
Math.max(x,y,z) 取最大值
Math.min(x,y,z) 取最小值
Math.random() 生成一個(gè)0到1之間的偽隨機(jī)數(shù)
2.string字符串
var s = 'hello,world';
s.charAt(0) 獲取索引為0的字符, 此處返回'h'
s.charCodeAt(0) 獲取索引為0的字符編碼
s.substring(1,4) 從索引位置截取子串, 此處返回'ell'
s.slice(1,4) 同上, 返回'ell'
s.slice(-3) 取最后三個(gè)字符, 此處返回'old'
s.indexOf('l') 第一次出現(xiàn)字符l的位置, 此處返回2
s.lastIndexOf('l') 最后一次出現(xiàn)字符l的位置, 此處為9
s.split(',') 用字符','來(lái)將字符串分隔為數(shù)組, 此處返回?cái)?shù)組['hello','world']
s.replace('h','H') 將'h'替換為'H', 此處返回'Hello,world'
s.toUpperCase() 返回大寫(xiě)的形式, 此處返回'HELLO,WORLD'
1.數(shù)學(xué)運(yùn)算
(1).一元運(yùn)算符+ -, string,boolean轉(zhuǎn)為number類型
var a = "1";
console.log(typeof a); // string
console.log(typeof +a); // number
console.log(+true); // 1
console.log(+false); // 0
(2).加法運(yùn)算, number會(huì)轉(zhuǎn)為string
console.log(+"50" + 1); // 結(jié)果51, +"50"會(huì)自動(dòng)轉(zhuǎn)為50
console.log("50" + 1); // 結(jié)果501
console.log("50" + "1"); // 結(jié)果501
console.log("50" + "a"); // 結(jié)果50a
(3).其余數(shù)學(xué)運(yùn)算, string會(huì)轉(zhuǎn)為number
console.log("50" - 1;) // 結(jié)果49
console.log("50" - "1";) // 結(jié)果49
console.log("50" * "2";) // 結(jié)果100
console.log("50" * "a";) // 結(jié)果NaN
2.關(guān)系運(yùn)算
(1).字符串和數(shù)字比較,字符串轉(zhuǎn)成數(shù)字
console.log("123" > 3) // true "123"轉(zhuǎn)成123
console.log("a" > 1) // false "a"轉(zhuǎn)成NaN
console.log("a" < 1) // false "a"轉(zhuǎn)成NaN
console.log("a" == 1) // false "a"轉(zhuǎn)成NaN
(2).字符串和字符串比較,每個(gè)字符都轉(zhuǎn)成ascii碼,然后一一比較
console.log("123" > "3") // false
console.log("abc" > "ab") // true
(3).漢字比較,每個(gè)字符轉(zhuǎn)成Unicode碼(0-65535之間整數(shù))
console.log("中".charCodeAt(0)); // 20013
console.log("國(guó)".charCodeAt(0)); // 22269
console.log("中" < "國(guó)"); // true
3.boolean布爾
布爾類型表示真或假,只有兩個(gè)值,true和false
JavaScript任意類型都可以轉(zhuǎn)換為boolean布爾值,轉(zhuǎn)換規(guī)則如下:
number => 除了+0,-0,NaN,其它都是true
string => 除了空字符串"",其它都是true
Object => 都是true
+0,-0,NaN,"",null,undefined => false呛伴,剩下的全都是true
例如:
if(0 || -0 || NaN || "" || null || undefined)
console.log('至有一個(gè)是true');
else
console.log('全都是false');
1.等于==, 只比較內(nèi)容
console.log(0 == true); // false
console.log(1 == true); // true
console.log(NaN == NaN); // false 凡是NaN參與判斷運(yùn)算符,除了! !=,其余全是false
console.log(0 == null); // false null轉(zhuǎn)成NaN
console.log(null == undefined); // true undefined轉(zhuǎn)成null
2.全等于===, 除了比較內(nèi)容,還要比較類型
console.log(1 === true); // false 內(nèi)容相同,類型不同
console.log(1 === "1"); // false 內(nèi)容相同,類型不同
console.log(null === undefined); // false 內(nèi)容相同,類型不同
簡(jiǎn)書(shū): http://www.reibang.com/p/9319e39dae0f
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/78118650
GitHub博客: http://lioil.win/2017/09/27/js-var.html
Coding博客: http://c.lioil.win/2017/09/27/js-var.html