JavaScript基础(1)

今天总结一些js最基础的知识:数据类型

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>js学习</title>
</head>

<body>
  <script>

    alert('hello,world') // 弹出消息框
    console.log('hello,world') // 控制台输出,f12查看

    //基本数据类型与方法
    let num = 1 // 数字number
    let str = ' hello,world ' // 字符串string
    let bol = true // 布尔
    let und = undefined // 未定义
    let obj = null // 空对象console.log(str.charAt(1))// 得到a位置字符 
    console.log(str.substring(1, 5))// 提取子串 ,索引a,索引b       
    console.log(str.substr(1, 5))// 提取子串,索引a,长度b        
    console.log(str.slice(1, 5))// 提取子串 ,索引a,索引b         
    console.log(str.toUpperCase())// 将字符串变为大写
    console.log(str.toLowerCase())// 将字符串变为小写
    console.log(str.indexOf('h'))// 检索字符在字符串的索引      
    console.log(str.trim())// 删除首尾空格    
    console.log(str.trimStart())// 删除首部空格    
    console.log(str.trimEnd())// 删除尾部空格   
    console.log(Number('123')) // 转数字
    console.log(parseInt('3.1415')) // 转整数
    console.log(parseFloat('3.14')) // 转小数
    console.log(String(123)); // 转字符串
    console.log(num.toString()); //转字符串

    //复杂数据类型和方法
    let star = {
      name: 'cxk',
      age: 18,
      hobbt: 'sing'
    } // 对象
    let arr = [1, 2, 3, 4] // 数组
    arr.push(5) // 在尾部插入新项 
    arr.unshift(0) // 在头部插入新项 
    console.log(arr);
    arr.pop() // 在尾部删除
    arr.shift() // 在头部删除
    console.log(arr);  
    console.log(arr.length) // 数组长度
    console.log(arr.slice(0, 1));
    arr.forEach(function (item, index, array) {
      console.log(`${item} 的索引值为:${index},在数组:${array} 中`);
    });
    let result = arr.map(function (item, index, array) {
      return `${item} 的索引值为:${index},在数组:${array} 中`
    })
    console.log(result);
    console.log(arr.join(','))
    console.log('a,bc,d'.split(','));
    
    //数学方法
    console.log(Math.pow(2, 3)) // 平方
    console.log(Math.sqrt(81)) // 开根号
    console.log(Math.ceil(2.4)) // 上取整
    console.log(Math.floor(2.4)) // 下取整

  </script>
</body>

</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值