js 中的算術運算


Math.pow(2,53)      // => 9007199254740992: 2 的 53次冪  
Math.round(.6)      // => 1.0: 四舍五入  
Math.ceil(.6)       // => 1.0: 向上求整  
Math.floor(.6)      // => 0.0: 向下求整  
Math.abs(-5)            // => 5: 求絕對值  
Math.max(x,y,z)         // 返回最大值  
Math.min(x,y,z)         // 返回最小值  
Math.random()       // 生成一個大於等於0小於1.0的偽隨機數  
Math.PI             // π: 圓周率  
Math.E              // e: 自然對數的底數  
Math.sqrt(3)            // 3的平方根  
Math.pow(3, 1/3)        // 3的立方根  
Math.sin(0)             // 三角函數: 還有Math.cos, Math.atan等  
Math.log(10)            // 10的自然對數  
Math.log(100)/Math.LN10     // 以10為底100的對數  
Math.log(512)/Math.LN2  // 以2為底512的對數  
Math.exp(3)             // e的三次冪 

 js 日期和時間 

var then = new Date(2011, 0, 1); // 2011年1月1日  
var later = new Date(2011, 0, 1, 17, 10, 30);// 同一天, 當地時間5:10:30pm,  
var now = new Date(); // 當前日期和時間  
var elapsed = now - then; // 日期減法:計算時間間隔的毫秒數  
later.getFullYear() // => 2011  
later.getMonth() // => 0: 從0開始計數的月份  
later.getDate() // => 1: 從1開始計數的天數  
later.getDay() // => 5: 得到星期幾, 0代表星期日,5代表星期一  
later.getHours() // => 當地時間17: 5pm  
later.getUTCHours() // 使用UTC表示小時的時間,基於時區 

js 字符串處理  

var s = "hello, world"  // 定義一個字符串  
s.charAt(0)             // => "h": 第一個字符  
s.charAt(s.length-1)        // => "d": 最后一個字符  
s.substring(1,4)        // => "ell":第2~4個字符  
s.slice(1,4)            // => "ell": 同上  
s.slice(-3)             // => "rld": 最后三個字符  
s.indexOf("l")      // => 2: 字符l首次出現的位置  
s.lastIndexOf("l")      // => 10:字符l最后一次出現的位置  
s.indexOf("l", 3)       // => 3:在位置3及之后首次出現字符l的位置  
s.split(", ")       // => ["hello", "world"] 分割成子串  
s.replace("h", "H")         // => "Hello, world": 全文字符替換  
s.toUpperCase()  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM