js資料類型及方法

來源:互聯網
上載者:User

標籤:object   spl   最大   The   單個字元   分割   bre   null   des   

資料類型及方法資料類型
  1. number 不區分整數和浮點數
  2. string 字串
  3. boolean true / false 布爾
  4. object null 數組
  5. function 函數
  6. undefined 未定義

    number方法
    method DES
    Number() 建構函式,參數必須全為數字,否則返回NaN(Not a Number)
    parseInt() 將參數轉換為數字,整數部分遇到不是數字就停
    parseFloat() 講參數轉換為數字,遇到不是數就停。
    toFixed() 保留小數(四捨五入),返回字串
    註:NaN資料本身不相等,類型為Number
var str = "101.123px";var n = Number(str);alert(isNaN(N));//truealert( n == n );//false
Math數學函數
Method DES
Math.pow() 求次方
Math.round() 四捨五入到整數
Math.ceil() 向上取整
Math.floor() 向下取整
Math.min() 取最小值
Math.max() 取最大值
Math.random()*n 取0-n的隨機數
var n = Math.random()*10 + 20//取10到30之間隨機數parseInt(n)取整
string 方法
Method DES
str[index] 取單個字元 ———— 相容IE8+
str.charAt( index ) 取單個字元 ———— 相容所有*
Number.toString() 轉換數字為字串
str.toUpperCase() 將字串字母變為大寫
str.toLowerCase() 將字串字母變為小寫
str.indexOf ("想要尋找文本", 查詢的起始位置)
str.substring() 截取字串(參數之間會做比較,哪個參數小放前邊, 參數為負數的時候,變為0)
str.slice() 截取字串(參二不能小於參一)
str.split("要以它作為分割的文本") 返回數組
Boolean布爾值

判斷條件真假(true:1/flase:0)
五個假語句如下,其餘都為真,包括Null 物件、空數組

undefinednull0NaN""

註:null 和 undefined

null轉換為數位時候 為0,在做條件判斷為假   類似於正確空值的填補  undefined 轉換為數字 為NaN,在做條件判斷為假   類似於錯誤空值的填補null 關鍵字, 非對象undefined ==> undefined1+undefined ==> NaN1+null      ==> 1
js運算子
DES SIGN
運算子 + - * / %
賦值運算 = += -= /= %=
判斷條件符號 > < == <= >= !=不等於 !== 不全等於 ===恒等於,還會判斷資料類型
邏輯運算子 與&& 或|| 非!

能被計算的時候才會執行計算,不能計算則會出現各種問題 (報錯 NaN);

  1. 除了加號 - * / % 會強制將參數轉換為數字
  2. 加法運算 只要有一個為字串,相加則是字串拼接
  3. &&條件遇到假就停,返回flase
  4. ||條件遇到真就停,返回true
for迴圈
for(var i=0; i<10; i++){            if( i==5 ){    //continue;跳過本次迴圈,到迴圈尾      break;//中斷迴圈            }     console.log(i);}for(var key in document) {        console.log(key +"======"+document[key]); // 查看document裡面所有的方法    }

流程式控制制

if ( 10>1 ) {        alert(1);    }else if( 5<50 ){        alert(2);    }else if( 10>20 ){        alert(3);    }    // 只有一條真語句    if( 8 > 0 ) alert(‘This is 8 > 0‘);

js資料類型及方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.