`

JavaScript入门指南_2

    博客分类:
  • JS
阅读更多

 JavaScript_2

 JS中注释:

JS中的注释同java中的注释:

1,采用  //  单行注释

2,采用  /*  **  */  多行注释

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>js_1</title>
</head>
<body>
	<noscript>您的浏览器禁用了JS功能</noscript>
	<script type="text/javascript" src="js/js_1.js"></script>
	<script>
		//单行注释
		// alert("hello,js_2");
		//多行注释
		/*alert("hello,js_2");*/
	</script>
</body>
</html>

 

 变量:

定义变量在JS中统一使用 var 无论什么类型的都使用此关键字

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>js_2</title>
</head>
<body>
	<script>
		var msg="hello,js_2";
		alert(msg);
	</script>
</body>
</html>

 

//JS中可以同时声明多个变量,使用逗号隔开,可以赋值,也可以不赋值
var m1,m2="hi",m3=12;          //js中类型能自动转换
alert(m1);//undefined     未赋值的
alert(m2);//hi             String类型
alert(m3);//12             int类型

 在函数中使用var关键字声明的是局部变量,如果不使用var声明,则是全局变量,推荐所有的变量声明均使用var关键字。

function sayHi(){
	var msg1="hello,js_2";
	alert(msg1);
}
function sayHello(){
	msg2="hello,js";
	alert(msg2);
}
sayHi();//hello,js_2
sayHello();//hello,js
alert(msg2);//hello,js
alert(msg1);//error   Uncaught ReferenceError: msg1 is not defined

 

 数据类型:

JS中的基本数据类型如下:

undefined

null

boolean

number

string

复杂数据类型:

Object

 

typeof用于检测变量的数据类型:

function sayHi(){
	var msg1="hello,js_2";
	alert(msg1);
}
var m1=sayHi,m2=12,m3="hello",m4=true,m5=null,m6;
alert(typeof m1);//function
alert(typeof m2);//number
alert(typeof m3);//string
alert(typeof m4);//boolean
alert(typeof m5);//object
alert(typeof m6);//undefined
alert(typeof m7);//undefined  未声明的变量执行typeof仍返回undefined

 

undefined:

var m1,m2=undefined;
alert(typeof m1);//undefined
alert(typeof m2);//undefined
alert(typeof m3);//undefined
alert(m1==undefined);//true

 

null:

var user=null;
alert(user);//null
alert(user==undefined);//true
alert(typeof user);//object
alert(user===undefined);//false

 undefined派生自null,所以进行boolean判断时,总是true,但是进行全等判断,就是false

 

boolean:

boolean类型只有两个值:true    和    false

使用Boolean()可以将任何类型的数据转换为true  or  false

下面的类型会被转化为false,其余的类型值全是true

 

alert(Boolean(false));//false

alert(Boolean(0));//false
alert(Boolean(NaN));//false

alert(Boolean(""));//false

alert(Boolean(null));//false

alert(Boolean(undefined));//false

 

number:

表示整型和浮点型:

var num1=100;//100
var num2=070;//8进制   56
var num3=0xA;//16进制  10

var num4=1.1;//1.1
var num5=3.125e5;//312500
var num6=2.0;//2

 

NaN:

NaN属于number类型,但是它的意思但是 not a number  不是一个数值    是number类型中的很特殊的一个值。

alert(typeof NaN);//number
alert(NaN===NaN);//false

 使用isNaN()方法可以用来判断变量是否可以转换为数值,如果可以转换则返回false,否则返回true

alert(isNaN(null));//false     可以转换为数值,false
alert(isNaN(100));//false
alert(isNaN(""));//false
alert(isNaN("100"));//false
alert(isNaN(1.2));//false
alert(isNaN(true));//false
alert(isNaN(undefined));//true
alert(isNaN(NaN));//true
alert(isNaN("12abc"));//true
alert(isNaN("abc"));//true

 

总结:

  • 注释
  • 变量
  • 基本数据类型
  • typeof
  • undefined
  • boolean
  • number
  • null
  • NaN
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics