一、JavaScript简介
js
ECMAScript简介
ES5、ES6
二、常见输出方式
1、弹出框输出
alert("要输出内容");
alert()
: 显示一个消息,并等待用户点击"确定"。confirm()
: 显示一个带有"确定"和"取消"按钮的对话框,并返回用户的选择。prompt()
: 显示一个对话框,包含一条提示信息以及用于用户输入文本的输入框和"确定"/"取消"按钮。
2、页面输出
document.write("要输出内容");
document.write()
: 直接向网页写入HTML表达式或JavaScript代码片段。- 修改DOM元素的
innerHTML
或textContent
属性:用于改变或添加元素内容。- 创建新的DOM元素并插入到页面中:使用
document.createElement()
和appendChild()
等。
3、控制台输出
控制台输出是JavaScript中常见的调试手段,主要通过console
对象提供的一系列方法来实现。例如:
console.log()
: 输出普通消息console.error()
: 输出错误消息console.warn()
: 输出警告消息console.info()
: 输出信息性消息console.debug()
: 输出调试消息
三、六种数据类型
1、数据类型分类
数值、字符串、布尔、对象(object)、null、undefined
数值、字符串、布尔又称为基本数据类型,对象是复合数据类型,null和undefined是两种特殊的数据类型。
2、null与undefined
二者都可以表示“没有”,含义非常相似,语法效果几乎没区别,历史原因。
null一般代表对象“没有”,undefined一般代表数值“没有”。
3、typeof运算符
typeof用于判断数据类型,一般使用方法如下:
Array.isArray方法返回一个布尔值,表示参数是否为数组,弥补typeod运算符的不足
var arr=['a','as','abd']
Array.isArray(arr) // true
四、运算符
非布尔值取反
一下六个值取反后为true,其他为false
undefined、null、false、0、NaN、空字符串(‘’)
五、字符串
1、charAt()方法
返回指定位置的字符,参数从0开始编号
例: var str = new String ('string');
s.charAt(2) // "r"
s.charAt(s.length - 1) // "g"
如果参数为负数或者超出字符串长度返回空字符串
s.charAt(-1) // ""
s.charAt(67) // ""
2、concat()方法
用于连接两个字符串,返回一个新的字符串,不改变原字符串
var s1 = ‘th’
var s2 = ‘is’
s1.concat(s2) // 'this'
s1 // 'th'
该方法可以接受多个参数
‘is’.concat('this' , 'yours') // "isthisyours"
如果参数不是字符串,concat方法会先将其转为字符串,然后再连接
使用 + 连接字符串
var result = s1 + s2
console.log(result); // this
concat与+区别:
concat遇到数字和字符串直接连接,而加号遇见数值先进行加法运算再和字符串进行连接。
3、substring()方法
用于从源字符串取出子字符串并返回,不改变源字符串。参数取值范围为(0,s.length-1)它的第一个参数表示字符串开始的位置,第二个参数表示结束位置(返回结果不包含第二个参数位置)
如果第二个参数大于第一个参数,substring方法会自动调换两个参数的位置,默认为“左小右大”
'helloworld'.substring(0,2) // 'he'
'helloworld'.substring(2,0) // 'he'
如果省略第二个参数,则表示字符串从第一个参数起始(包含该位置字符)一直到字符串末尾
'helloworld'.substring(2) // 'lloworld'
如果参数为附属,substring方法会自动将负数转为0
'helloworld'.substring(-3) // 'helloworld'
'helloworld'.substring(2,-3) // 'he'
4、substr()方法
用于从源字符串去除子字符串并返回,不改变原字符串,跟substring方法的作用相同
substr方法的第一个参数是子字符串的开始位置(从0开始计算),第二个参数是子字符串的长度
‘helloworld’.substr(2,5); // llowo
如果省略第二个参数,则表示子字符串一直到字符串结束
‘helloworld’.substr(5); // world
如果第一个参数是负数,表示倒数计算的字符位置,如果第二个参数是负数,将被自动转为0,因此返回空字符串
‘helloworld’.substr(-6); // oworld
‘helloworld’.substr(2,-1); // “”
5、indexOf()方法
用于确定一个字符串在另一个字符串中第一次出现的位置,返回结果是匹配开始的位置(0,s.length-1)。如果返回-1,就表示不匹配。
“hello world”.indexOf('o') // 4
'helloworld'.indexOf('sl') // -1
如果加上第二个参数,表示从该位置开始向后匹配第一个参数所在的位置。
“hello world”.indexOf('o',6) // 7
6、trim()方法
用于去除字符串两端的空格,包括制表符(\t、\v)、换行符(\n)、回车符(\r)返回一个新字符串,不改变源字符串,
“ hello world ”.trim() // "hello world"
“\r\nhello world \t”.trim() // "hello world"
ES6扩展方法,trimEnd()和trimStart()方法
“ hello world ”.trimEnd() // " hello world"
“ hello world ”.trimStart() // "hello world "
7、split()方法
按照给定规则分割字符串,返回一个由分割出来的字符串组成的数组;如果分割规则为空字符串,则返回原字符串的每一个字符;如果省略参数,则返回数组的唯一成员——原字符串
'hello|world'.split('|') // ["hello","world"]
'a|b|cd'.split('') // ["a","|","b","|","c","d"]
'hello|world'.split() // [hello|world]
split方法可以接受第二个参数,用于限制返回数组的组大成员数
'hello|world'.split('|',0) // []
'hello|world'.split('|',1) // ["hello"]
'hello|world'.split('|',2) // ["hello","world"]
文章为本人学习笔记,欢迎各位大佬指正交流