Javascript 字符串、数字、数组

web前端开发

共 2272字,需浏览 5分钟

 ·

2021-10-02 17:09

英文 | https://programmer-anamul.medium.com/javascript-string-number-array-18378f26c692
翻译 | 杨小二

Javascript 是最流行的编程语言。目前,Facebook、YouTube 等大多数大型网站都在使用 JavaScript。所以,让我们学习一些关于 JavaScript 字符串、数字和数组的基本知识。
String
JavaScript 字符串用于存储和操作文本。JavaScript 字符串存储了一系列字符,如“Anamul Hoque”。
Javascript 提供了一些方法来处理 Javascript 字符串,如下所示:
01、String.length()
String.length() 方法用于查找字符串的长度,使用内置的 length 属性。
const name = "Hey I am Anamul Hoque";console.log(name.length)

02、string.charAt():

string.charAt() 方法返回字符串中指定索引处的字符。第一个字符的索引为 0,第二个字符的索引为 1,以此类推。字符串中最后一个字符的索引是 string.length-1。

const name = "Hey I am Anamul Hoque";const result = name.charAt(str.length-1);console.log(result)

03、string.concat():

string.contact() 方法用于连接两个或多个字符串。

const fristName = "Md Anamul "const lastName = "Hoque"const fullName = fristName.conact(lastName)console.log(fullName)

04、string.includes():

string.includes() 方法确定字符串是否包含指定字符串的字符。结果总是返回 true 或 false 值。

const name = "Hi, I am Anamul Hoque"const result = name.includes("Anamul") //trueconst result2 = name.includes("Bangladesh") // false

Number

Number 是 javascript 的一种数据类型,它用于表示和操作数字,如 67 或 -9, 0.4 。

Javascript 提供了一些使用 Javascript Number 的方法。如下所示:

01、Math.abs():

abs() 方法返回数字的绝对值。

const number = -4.76const result = Math.abs(number);console.log(result)

02、Math.random():

Math.random() 方法返回一个从 0 到1,但不包括 1 的随机数。

const number = Math.random();console.log(number)

03、Math.round():

Math.round() 方法将数字四舍五入到最接近的整数。

const number = Math.round(8.97876);console.log(number)

04、Math.sqrt():

Math.sqrt() 方法用于求一个数的平方根。

const number = Math.sqrt(99);console.log(number)

Array:

数组是一种特殊类型的对象,用于在单个变量中存储多个值。

Javascript 提供了一些方法来处理 Javascript 数组,如下所示:

01、array.concact():

array.contact() 方法用于合并两个或多个数组。

const array1 = ['a', 'b', 'c'];const array2 = ['d', 'e', 'f'];const result = array1.concat(array2);console.log(result)

02、array.filter():

array.filter() 方法创建一个数组,其中填充了作为函数通过测试的所有数组元素。

const ages = [12,13,24,12,56,32]const result = age.filter(age => age > 20)console.log(result)

03、array.map():

array.map() 方法使用为每个数组元素调用函数的结果创建一个新数组。

const ages = [12,13,24,12,56,32]const restlt = ages.map(age => age.age)console.log(result)

除了这些,还有一些其他的方法。 

今天内容就分享到这里,感谢你的时间,谢谢阅读!


学习更多技能

请点击下方公众号

浏览 12
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报