当前位置: 首页 > news >正文

11-14强制类型转换

强制类型转换String

<!DOCTYPE html>
<html lang="zh-CH"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>// 将其他类型转换为String类型// 方法一:调用被转换数据类型的toString方法,null和undefined不行var a = 123;var b = a.toString();document.write(typeof b);document.write(b);//方法二:调用String函数c = String(a);document.write(typeof c);document.write(c);var d = null;e = String(d);document.write(typeof e);document.write(e);</script>
</body></html>

强制类型转换Number

<!DOCTYPE html>
<html lang="zh-CH"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>// 将其他类型转换为Number类型//方法一:调用Number函数a = 123;b = Number(a);document.write(typeof b);document.write(b);//有非数字,则转换成NaNc = "123abc";d = Number(c)document.write(typeof d);document.write(d);//为空值或一个空格,转换成0var e = null;f = Number(e);document.write(typeof f);document.write(f);//为布尔型,转为数字,0,1var g = true;h = Number(g);document.write(typeof h);document.write(h);//调用parseint()函数将a转换为Number,读到非数字结束j=parseInt(g);document.write(typeof j);document.write(j);</script>
</body></html>

其他进制的数字

<!DOCTYPE html>
<html lang="zh-CH"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>//其他进制的数字a = 123;//16进制a=0x10;//8进制a=070;document.write(a);</script>
</body></html>

转换为Boolean

<!DOCTYPE html>
<html lang="zh-CH"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>//将其他类型转换为Booleanvar a=123;//调用Boolean()函数a=Boolean(a);//除了0和NaN,其余都是truedocument.write(typeof a);document.write(a);b=true;c=Boolean(b);document.write(typeof c);document.write(c);e=NaN;f=Boolean(e);document.write(typeof f);document.write(f);</script>
</body></html>

 

http://www.dtcms.com/a/532854.html

相关文章:

  • redis中的数据类型
  • 2025年10月25日(星期六)骑行哈马者游记
  • 数据结构 —— 堆
  • 基于阿里云SDK的DDNS系统:架构设计与性能优化实战
  • 有哪些做调查问卷赚钱的网站自行创建网站的平台
  • 丰南建设网站wordpress网站前台打开慢
  • C4D域力场的修改层及遮罩详解
  • SAP SD交货单过账冲销接口分享
  • 如何在 Java 程序中检查是否可以连接到某个网站
  • 做网站市场报价步登顶wordpress小程序百家号
  • C语言复习笔记(一)
  • (10)shell 编程入门
  • 深入剖析C++文件操作的底层机制
  • 高端网站价格网架球加工厂家
  • 响应式网站文章网站建设字体变色代码
  • python+vue图书在线阅读推荐平台设计(源码+文档+调试+基础修改+答疑)
  • git仓库基本使用
  • 网站维护需要多久时间上海做网站建设公司排名
  • 在婚恋网站上做红娘怎么样什么都可以看的浏览器
  • 做平台网站要什么条件重庆网站建设重庆网站设计
  • 江苏系统建站怎么用咖啡网页设计素材
  • 网站建设软件免费使用密码访问wordpress文章
  • 物联网设备通信协议参数的能耗自适应调整机制
  • PySide6 win10 记事本从零到一——前言
  • win7 iis网站无法显示石家庄尚武科技
  • 建设网站哪家公司好网站建设公司招聘
  • UFS Reset 介绍
  • ESP8266无线开关
  • 生鲜电商网站建设一共有多少网站
  • 泰州网站建设案例海淘网站主要关键词