1. 循环
1.1 语法格式
for(i = 0; i < 10; i++){console.log(i);//执行语句}/****/var i = 0;while(i < 10){console.log(i);i++;}
1.2 基础算法
/*1.不使用break或continue语句,当i>10时退出for循环*/var i = 1;for(;i;){console.log(i)i++;if(i>10){i=0;}}/*2.从0开始相加,总和大于100时结束*/var sum = 0;for(var i = 0; i < 100; i++){sum += i;if(sum > 100){break;}console.log(sum,i);}/*3.100以内的数跳过可以被7整除或个位数为7的数*/for(var i = 0; i <= 100; i++){if (i % 7 == 0 || i % 10 == 7){continue;}console.log(i);}/*4.打印0-100的数,要求:()只能有一句,不能写比较{}不能出现i++,i--*/var i = 101;for(;i--;){console.log(i);}/*5.10的n次方*/var n = 5;var sum = 1;for(var i = 0; i < n;i ++){sum *= 10;}console.log(sum);/*6.n的阶乘*/var n = 5;var num = 1;for(var i = 1; i <= n; i++){num*=i;}console.log(num);/*7.打印三个数中最大的数*/var a = 1,b = 2,c = 3;console.log((a > b ? a : b) > c ? (a > b ? a : b) : c);/*8.打印100以内的质数*/var count = 0;for(var i = 2; i <= 100; i++){for(var j = 1; j <= i; j++){if(i % j == 0){count++;}}if(count == 2){console.log(i);}count = 0;}
2.引用值
2.1 种类
- array
- object
- function
- date
-
2.2 数组
语法格式
var arr = [1, 2, 3, 4, 5, 6, 7];
⭐
语法格式
var person = {//属性名/键名:属性值/键值name: '小夏',age: 18.job: 'WEB开发工程师'}
3.typeof()方法
3.1 使用方法
typeof()能测出的类型
- number
- string
- boolean
- object
- 具体的某一对象 -> object
- 引用类型 -> Object
具体测值:
typeof(123); //numbertypeof('123'); //stringtypeof(true); //booleantypeof({}); //objecttypeof([]); //objecttypeof(null); //object/*1.typeof(null) 返回 object 是一个历史遗留的bug,Js中的基本数据类型都存储在32位的二进制单元当中,低三位的数字用于表示该数据的类型。低三位数字和表示类型的对应关系如下:000: object - 当前存储的数据指向一个对象。1: int - 当前存储的数据是一个 31 位的有符号整数。010:double - 当前存储的数据指向一个双精度的浮点数。100: string - 当前存储的数据指向一个字符串。110: boolean - 当前存储的数据是布尔值。typeof的实现就是通过判断低三位的数字来判断值类型的。当传入一个null的时候,因为null存储时32位的数字表示都是0,所以低三位也是0,typeof就将其认定为object类型的了*/typeof(undefined); //undefinedtypeof(function(){}); //function/**/typeof(1 - 1); //numbertypeof(1 - "1"); //number/*未定义a*/typeof(a); //undefinedtypeof(typeof(a)); //string
4. 显式、隐式类型转换
4.1 显示
Number()
var a = '123';typeof(Number(a) + ' - ' + Number(a)); // number - 123a = true;typeof(Number(a) + ' - ' + Number(a)); // number - 1a = null;typeof(Number(a) + ' - ' + Number(a)); // number - 0a = undefined;typeof(Number(a) + ' - ' + Number(a)); // number - NaNa = 'a';typeof(Number(a) + ' - ' + Number(a)); // number - NaNa = '1a';typeof(Number(a) + ' - ' + Number(a)); // number - NaNa = 3.14;typeof(Number(a) + ' - ' + Number(a)); // number - 3.13
parseInt
var a = '123';typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 123a = ture;typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaNa = null;typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaNa = undefined;typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaNa = NaN;typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaNa = '3.14';typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 3a = '3.99';//不是四舍五入typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 3a = 'abc123';typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaNa = '123abc';typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 123/*双参数*/parseInt(a, 16); //代表以16进制为基础转换a,参数可取2-36
parseFloat
var a = '3';parseFloat(a);//3a = '3.141592654'parseFloat(a.toFixed(2));//保留2位小数,且四舍五入
String()
var a = 123;typeof(String(a) + ' - ' + String(a);//string - '123'
String() 和 toString()
- toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined
- toString() 括号中的可以写一个数字,代表进制,对应进制字符串
二进制:.toString(2);
八进制:.toString(8);
十进制:.toString(10);
十六进制:.toString(16); - String()可以将null和undefined转换为字符串,但是没法转进制字符串
Boolean()
Boolean(1);//trueBoolean(null);//false/*undefined,null,NaN,"",0,false 都是false,其他为true*/
4.2 隐式
案例 ```javascript var a = ‘123’; a++; console.log(a); //1234
a = ‘a’ + 1;//String(1); console.log(a);//a1
a = ‘3’ * 2;// & / - % 则 str -> number console.log(a);//6
a = ‘1’ > 2; console.log(a);//false
a = 1 > ‘2’;//Number() console.log(a);//flase
a = ‘a’ > ‘b’;//ascii console.log(a);//false
a = 1 == ‘1’; console.log(a);//true
a = 1 === ‘1’;//不进行隐式转换 console.log(a);//false
a = NaN == NaN; console.log(a);//flase
a = undefined > 0;//=’ ‘<’ -> false console.log(a);//false
a = null > 0; //‘=’ ‘<’ -> false console.log(a);//false
a = undefined == null; console.log(a);//true / undefined 值是派生自 null 值的,ECMAScript 标准规定对二者进行相等性测试要返回 true,可以理解为 null 和 undefined 都代表着无效的值,所以二者相等,但由于是两种不同的原始数据类型,所以不全等。 /
var a1 = 2 > 1 > 3; var a2 = 2 > 1 == 1; console.log(a1,a2);//false true
2. isNaN```javascript/*isNaN -> Number(值)*/isNaN(NaN);//trueisNaN(123);//falseisNaN('123');//false 经过隐式转换isNaN('a');//trueisNaN(null);//falseisNaN(undefined);//ture
