JavaScript(类型转换、条件语句、循环、函数)
类型装换
转为数字类型
// Number
console.log(Number(undefined)); //NaN
console.log(Number(null)); //0
console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(Number('123')); //123
console.log(Number('123a')); //NaN
console.log(Number('1.23')); // 1.23
console.log(Number('+123')); //123
console.log(Number('-123')); // -123
console.log(Number('1+23')) // NaN
console.log(Number('')); // 0
console.log(Number(' ')); // 0
console.log(Number('1 23'));// NaN
console.log(Number('1$%23')); // NaN
console.log(Number(' 123')); //123
console.log(Number('1.2.3')); // NaN
console.log(Number('.23')); // 0.23
// parseInt() 会试图将字符串转为整型,如果失败 返回NaN
// 如果前面是空格 数字正负号时, 当碰到 第一个不是数字时停止装换
// 如果不是数字空格直接返回NaN
console.log(parseInt(undefined)); //NaN
console.log(parseInt(null)); // NaN
console.log(parseInt(true)); // NaN
console.log(parseInt(false)); // NaN
console.log(parseInt('123')); //123
console.log(parseInt('123a')); // 123
console.log(parseInt('1.23')); // 1
console.log(parseInt('+123')); //123
console.log(parseInt('-123')); // -123
console.log(parseInt('1+23')) // 1
console.log(parseInt('')); // NaN
console.log(parseInt(' ')); // NaN
console.log(parseInt('1 23'));// 1
console.log(parseInt('1$%23')); // 1
console.log(parseInt(' 123')); //123
console.log(parseInt('1.2.3')); // 1
console.log(parseInt('.123')) //NaN
// parseFloat 与parseInt没有太大区别 除了以下几条
console.log(parseFloat('1.23')); // 1.23
console.log(parseFloat('1.2.3')); // 1.2
console.log(parseFloat('.123')) // 0.123
转为String 类型
// undefined null boolean 转为String类型
var a = undefined + '';
console.log(typeof a); // string
var b = null + '';
console.log(typeof b); // string
var c = true.toString();
console.log(typeof c);
var num = 20;
var d = num.toString();
console.log(d); // 在浏览器控制台中字符类型是黑色的 数字是蓝色的
其他类型转为Boolean类型
console.log(Boolean('')); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(null)); // false
console.log(Boolean(0)); // false
console.log(Boolean(0.0)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(' ')); // true
console.log(Boolean(1)); // true
console.log(Boolean('abc')); // true
isNaN and isFinite
console.log(isNaN(NaN)); // true
console.log(isNaN('NaN')); // true
console.log(isNaN(123)); // false
// isFinite: 判断既不是NaN类型 也不是InFinite类型
console.log(isFinite(123)) // true
console.log(isFinite(NaN)); // false
console.log(isFinite(Infinity)); // false
console.log(isFinite(' ')) // true
console.log(isFinite('123 ')) // true
console.log(isFinite('123abc')) // false
条件语句
- if 语句
- 三目运算符
res = a>b?true:false(运行效率比if语句高) - switch语句
- default break 都是可选语句了,当 case 后面没有 break 语句时,如果条件匹配成功,直接贯穿所有 case 语句,直到执行 break 语句
循环语句
while
do while
for
for(var i = 0; i < array.lenght; i++)
for(var i in array) // i 是下标
for(var i of array) // i 是元素
break and continue
// js中循环是可以有名字的
// 语法格式 名称:循环语句
// break continue 可以跳过或终止 带有标签名的循环
label1 : for(var i = 0; i < 10; i++){
label2 : for(var j = 0; j < 10; j++){
if(j == 3){
break label1;
}
console.log(i + ' ' + j);
}
}
函数
函数的概述
- 函数名:建议使用驼峰规则
- 如果不写 return,系统默认返回 undefined
- 函数在调用时只要有该函数的声明即可
函数的参数
function info(name, age) {
document.write(name);
document.write(age);
}
info('lily', '9'); // lily 9
info('lily'); // lily undefined
info('lily','9','c','d') // lily 9
// js不定长参数
function printSum(){
sum = 0;
for(i in arguments)
sum += arguments[i];
return sum
}
console.log(printSum(1,2,3))
作用域
- 新的作用域:函数会产生新的作用域,循环,条件语句不会产生新的作用域
- 变量作用域
- 全局变量:定在文件中,但在函数外的变量为全局变量,使用范围为整个文件的任意位
- 局部变量:在函数内部使用var声明的变量为局部变量,使用范围为该函数内部
变量的提升
var num = 100;
/*
* 在js中如果函数内部使用var声明变量时,在代码执行过程中,系统
* 会自动把变量的声明语句(var )提到函数的最上方,这个操作叫做变量的提升
* */
function fun1 () {
console.log(num); // undefined
num = num +1
console.log(num); // NaN undefined + 1 = NaN
var num = 200
console.log(num); // 200
}
fun1();
参数默认值
// 定义一个函数,该函数有两个参数
// 参数的默认值
// 第一种方式: 在定义形参时,直接赋值
// 第二种方式: a = a||555;
// 第三种方式: a = a ? a : 333
function func2(a, b=999){
// a = a||555;
a = a ? a : 333
console.log(a);
console.log(b);
}
func2(1,2);
func2(4);
func2();
匿名函数
// 非即时函数
var a = function(num){
console.log(num);
};
a(1233);
// 即时函数
(function(num){
console.log(num);
})(666);
// 一般来说,匿名函数当做一次性函数使用,调用一次结束后,
// 一般情况下会把匿名函数作为即时函数,但是任何函数都可以
// 即时执行
// 普通函数形式的即时函数
/*(function abc(){
alert('123')
})();*/
JavaScript(类型转换、条件语句、循环、函数)的更多相关文章
- JavaScript的条件语句
JavaScript的条件语句 1.JavaScript的条件语句包括以下几个 (1)if - 只有当指定条件为true时,使用该语句来执行代码: (2)if...else - 当指定条件为true时 ...
- JavaScript if 条件语句
JavaScript if 条件语句 使用: if(条件){ }else if(条件){ }else if(条件){ }else{ } 示例: // 判断相等 if(1==1){ } // 判断不等 ...
- JavaScript case 条件语句
JavaScript case 条件语句 示例 switch(name){ case '1': age = 123; break; case '2' age = 456; break; default ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- Javascript关键字,条件语句,函数及函数相关知识
关键字 条件语句 作用域 回调 关键字 根据规定,关键字是保留的,不能用作变量名或函数名. 下面是一些ECMAScript关键字的完整列表. break ,case,catch,continue,de ...
- JavaScript基本语法 -- 条件语句 & 循环语句
条件语句 条件语句(Conditional statement)是JavaScript里面的基本结构之一,程序根据表达式的真假决定执行或者跳过某个分支,于是,条件语句有时候也可以称为"分支语 ...
- Python基础——循环语句、条件语句、函数、类
注:运行环境 Python3 1.循环语句 (1)for循环 注:for i in range(a, b): #从a循环至b-1 for i in range(n): #从0循环至n-1 ...
- python - 条件语句/循环语句/迭代器
条件测试:if 条件表达式python 的比较操作 所有的python对象都支持比较操作 可用于测试相等性.相对大小等 如果是复合对象,pyt ...
- 值类型之间的相互转化,运算符,if条件判断,循环,函数
值类型之间的相互转化 number | string | boolean 一.转换为boolean=>Boolean(a); var num = 10; var s = '123'; var b ...
- js基础(条件语句 循环语句)
条件语句 if语句块的语法形式如下: //只有两种情况下if(条件){要执行的语句块;}else{要执行的语句块;} //多种情况下if(条件){要执行的语句块;}else if(条件){要执行的语句 ...
随机推荐
- kill、killall、pkill杀手三人组
1.1 kill.killall.pkill杀手三人组 1.利用kill 进程号 方式杀掉rsync进程 [root@backup ~]# ps -ef |grep rsync root 3500 1 ...
- vue中嵌套页面 iframe 标签
vue中嵌套iframe,将要嵌套的文件放在static下面: <iframe src="../../../static/bear.html" width="300 ...
- Git Bash上传文件
今天通过Git Bash上传了一个项目(之前是通过Github Desk上传的),操作命令如下: 在目录下shift+右键打开Git Bash 1.git init 2.git add *.py 3. ...
- juqery 给本身的class加上一个class 或也可以实现关注商品,取消关注商品
$("#goods1").on("click",".ICON-fen-LOVE",function(){ var $this = $(thi ...
- 使用LibreOffice修复受损的Office文档
在工作中时常遇到Office文档损坏,用MS Office不能打开,有时候用LibreOffice(测试为4.2版本)可以打开,另存一下就好了. 此方法虽然不是100%管用,但在实际中大半都可以. 另 ...
- java.lang.NoClassDefFoundError 错误
练习jfianl,,,配置数据库插件的时候遇到: java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource 解 ...
- 倒计时48小时|2018GIAC上海站参会攻略来了!
再过一天,令大家期待已久的GIAC全球互联网架构大会将登陆魔都与众位架构师.技术负责人及高端技术从业人员见面! 这场策划许久的技术盛宴,我们邀请到了腾讯.阿里.京东.美团.keep.UC.360.网商 ...
- Presto实战
一.Presto简介 1.PRESTO是什么? Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持GB到PB字节. Presto的设计和编写完全是为了解决像Facebook ...
- CLOSE_WAIT状态的原因与解决方法(转载留自己看)
这个问题之前没有怎么留意过,是最近在面试过程中遇到的一个问题,面了两家公司,两家公司竟然都面到到了这个问题,不得不使我开始关注这个问题.说起CLOSE_WAIT状态,如果不知道的话,还是先瞧一下TCP ...
- ng2-tree
[转]https://github.com/valor-software/ng2-tree#eyes-demo demo:http://valor-software.com/ng2-tree/