javascript学习笔记之基本语法测试
/* -------------------------
Javascript 基本语法测试
----------------------------*/
/*
基础概念
1.区分大小写
2.变量是弱类型,命名是第一个字符是必须是字母,下划线(_)或美元符号($),余下字符可以是下划线,美元符号或任何字母或数字
3.变量可存放两种类型的值,原始值(Undefined,Null,Boolean,Numbew,String可用typeof 运算符判断一个值是否是原始类型)和引用值,
*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
//弱类型可随意赋值,好的编程习惯始终存入相同类型的值
var test = "hi";
writeline(test);
test = 55;
writeline(test);
//typeof运算测试
var notInitialization;
var asNull = null;
var aBool = true;
var aNumber = 10;
var aString = "Str";
writeline(typeof(notInitialization));//undefined
writeline(typeof(asNull));//object,null被认为是对象占位符,所以输出object,但本质上是原始类型
writeline(typeof(aBool));//boolean
writeline(typeof(aNumber));//number
writeline(typeof(aString));//string
//uedefined测试
var testUndefiend;//声明变量未初化时,默认为undefined
writeline(testUndefiend);//输出undefined
writeline(testUndefiend == undefined);//true
writeline(typeof(notDeclare));//undefined
function noReturnValue(){}
writeline(noReturnValue());//undefined,函数无返回值时,输出也是undefined
//Null测试
writeline(null == undefined);//true,值相等但含义不同,undefined表明声明了变量但未对其初始化,null表示尚未存在的对象
//Boolean类型测试
var istrue = true;
var isfalse = false;
writeline(istrue == 1); //true
writeline(isfalse == 0);//true
writeline(Number(istrue));//1
writeline(Number(isfalse));//0
//Number类型测试
//既可以表示32位的整数,还可以表示64位浮点数
var num1 = 55;
var num2 = 070; //八进制
var num3 = 0x1f; //十六进制
var num4 = 5.0; //浮点值,在运算之前保存为字符串
var num5 = 3.125e7;//科学记数法
var num6 = 0.0000000006;//ECMAScript默认把具有6个或6个以上前导0的浮点数转换成科学记数法
writeline(num1);//55
writeline(num2);//56
writeline(num3);//31
writeline(num4);//5
writeline(num5);//31250000
writeline(num6);//6e-10
//特殊Number值
writeline(Number.MAX_VALUE); //1.7976931348623157e+308
writeline(Number.MIN_VALUE); //5e-324
writeline(Number.NaN); //NaN
writeline(Number.NEGATIVE_INFINITY);//-Infinity
writeline(Number.POSITIVE_INFINITY);//Infinity
//Infinity
var multiple = Number.MAX_VALUE * Number.MAX_VALUE;
if (isFinite(multiple))
{
writeline("Number is finite.");
}
else
{
writeline("Number is infinite."); //Number is infinite.
}
//NaN Not a Number
var nonNumber = "test";
var aNaN = Number(nonNumber);
writeline(aNaN); //NaN 表示类型转换失败时
var addNaN = 1 + aNaN;
writeline(addNaN); //NaN NaN参与运算结果也是NaN
writeline(NaN == NaN);//false NaN与自身也不相等
writeline(isNaN(aNaN)); //true
//String
//Unicode编码,首字符从0开始,最后的字符位置为字符串长度-1.可由""或''声明.
var str1 = "test1";
var str2 = 'test2';
writeline(str1); //test1
writeline(str2); //test2
//转义字符
/*
\n 换行
\t 制表符
\b 空格
\r 回车
\f 换页符
\\ 反斜杠
\' 单引号
\" 双引号
\0nnn 八进制代码表示的字符
\xnn 十六进制代码表示的字符
\unnnn 十六进制表示的Unicode字符
*/
var testEscape = "start
"
testEscape += "\r\n";
testEscape += "\r\n" + "blank\bblank";
testEscape += "\r\n" + "tab\ttab";
testEscape += "\r\n" + "enter\renter";
testEscape += "\r\n" + "page\f";
testEscape += "\r\n" + "backslash \\";
testEscape += "\r\n" + "single quotation marks \' ";
testEscape += "\r\n" + "double quotation marks \" ";
testEscape += "\r\n" + "\0344";
testEscape += "\r\n" + "\x44";
testEscape += "\r\n" + "\u3456";
testEscape += "\r\n" +"End";
alert(testEscape);
Javascript 基本语法测试
----------------------------*/
/*
基础概念
1.区分大小写
2.变量是弱类型,命名是第一个字符是必须是字母,下划线(_)或美元符号($),余下字符可以是下划线,美元符号或任何字母或数字
3.变量可存放两种类型的值,原始值(Undefined,Null,Boolean,Numbew,String可用typeof 运算符判断一个值是否是原始类型)和引用值,
*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
//弱类型可随意赋值,好的编程习惯始终存入相同类型的值
var test = "hi";
writeline(test);
test = 55;
writeline(test);
//typeof运算测试
var notInitialization;
var asNull = null;
var aBool = true;
var aNumber = 10;
var aString = "Str";
writeline(typeof(notInitialization));//undefined
writeline(typeof(asNull));//object,null被认为是对象占位符,所以输出object,但本质上是原始类型
writeline(typeof(aBool));//boolean
writeline(typeof(aNumber));//number
writeline(typeof(aString));//string
//uedefined测试
var testUndefiend;//声明变量未初化时,默认为undefined
writeline(testUndefiend);//输出undefined
writeline(testUndefiend == undefined);//true
writeline(typeof(notDeclare));//undefined
function noReturnValue(){}
writeline(noReturnValue());//undefined,函数无返回值时,输出也是undefined
//Null测试
writeline(null == undefined);//true,值相等但含义不同,undefined表明声明了变量但未对其初始化,null表示尚未存在的对象
//Boolean类型测试
var istrue = true;
var isfalse = false;
writeline(istrue == 1); //true
writeline(isfalse == 0);//true
writeline(Number(istrue));//1
writeline(Number(isfalse));//0
//Number类型测试
//既可以表示32位的整数,还可以表示64位浮点数
var num1 = 55;
var num2 = 070; //八进制
var num3 = 0x1f; //十六进制
var num4 = 5.0; //浮点值,在运算之前保存为字符串
var num5 = 3.125e7;//科学记数法
var num6 = 0.0000000006;//ECMAScript默认把具有6个或6个以上前导0的浮点数转换成科学记数法
writeline(num1);//55
writeline(num2);//56
writeline(num3);//31
writeline(num4);//5
writeline(num5);//31250000
writeline(num6);//6e-10
//特殊Number值
writeline(Number.MAX_VALUE); //1.7976931348623157e+308
writeline(Number.MIN_VALUE); //5e-324
writeline(Number.NaN); //NaN
writeline(Number.NEGATIVE_INFINITY);//-Infinity
writeline(Number.POSITIVE_INFINITY);//Infinity
//Infinity
var multiple = Number.MAX_VALUE * Number.MAX_VALUE;
if (isFinite(multiple))
{
writeline("Number is finite.");
}
else
{
writeline("Number is infinite."); //Number is infinite.
}
//NaN Not a Number
var nonNumber = "test";
var aNaN = Number(nonNumber);
writeline(aNaN); //NaN 表示类型转换失败时
var addNaN = 1 + aNaN;
writeline(addNaN); //NaN NaN参与运算结果也是NaN
writeline(NaN == NaN);//false NaN与自身也不相等
writeline(isNaN(aNaN)); //true
//String
//Unicode编码,首字符从0开始,最后的字符位置为字符串长度-1.可由""或''声明.
var str1 = "test1";
var str2 = 'test2';
writeline(str1); //test1
writeline(str2); //test2
//转义字符
/*
\n 换行
\t 制表符
\b 空格
\r 回车
\f 换页符
\\ 反斜杠
\' 单引号
\" 双引号
\0nnn 八进制代码表示的字符
\xnn 十六进制代码表示的字符
\unnnn 十六进制表示的Unicode字符
*/
var testEscape = "start
![](https://www.cnblogs.com/Images/dot.gif)
testEscape += "\r\n";
testEscape += "\r\n" + "blank\bblank";
testEscape += "\r\n" + "tab\ttab";
testEscape += "\r\n" + "enter\renter";
testEscape += "\r\n" + "page\f";
testEscape += "\r\n" + "backslash \\";
testEscape += "\r\n" + "single quotation marks \' ";
testEscape += "\r\n" + "double quotation marks \" ";
testEscape += "\r\n" + "\0344";
testEscape += "\r\n" + "\x44";
testEscape += "\r\n" + "\u3456";
testEscape += "\r\n" +"End";
alert(testEscape);