JS-快速入门

变量

var let const
var是全局作用,所以基本不用
let是变量
const是常量,但是如果声明的是数组或对象时,是可以部分改变,但不能完全改变

原生数据类型

String Number Boolean null undefined
整型和浮点型都是Number类型
null和unfined区别为null是空,unfined是根本没有定义

const username = "Z";
const age = 30;
const isCool = true;
const rate = 4.5;
const x = unfined;
const y = null;

查看数据类型

const age = 30;
console.log(typeof age);

在这里插入图片描述

模板字符串

有两种方式
1.字符串+变量
2.使用`${变量}`

const age = 30;
console.log("My age is "+age);
console.log(`My age is ${age}`);

在这里插入图片描述

字符串的内置属性、方法

  • 字符串长度
  • 字符串转大小写
  • 字符串截取
  • 字符串转数组
const s = "Hello JavaScritp,yes";
console.log(s.length);
console.log(s.toUpperCase());
console.log(s.toLowerCase());
console.log(s.substring(1,3));
console.log(s.split(""));

在这里插入图片描述

数组

数组创建方式

1.使用newArray()

const numbers = new Array(1,2,3,4);
console.log(numbers);

在这里插入图片描述
2.使用[]声明

const fruits = ["apple","pears"];
console.log(fruits);

在这里插入图片描述

数组值操作

获取值

const fruits = ["apple","pears"];
console.log(fruits[1]);

在这里插入图片描述

添加值

1.在数组末尾添加值

const fruits = ["apple","pears"];
fruits.push("banana");
// console.log(numbers);
console.log(fruits);

在这里插入图片描述
2.在数组首部添加值

const fruits = ["apple","pears"];
fruits.unshift("banana");
// console.log(numbers);
console.log(fruits);
console.log(fruits[1]);

在这里插入图片描述

删除值

删除数组末尾元素

const fruits = ["apple","pears"];
fruits.pop();
console.log(fruits);

在这里插入图片描述

判断是否是数组

const fruits = ["apple","pears"];;
console.log(fruits);

在这里插入图片描述

获取值的索引

const fruits = ["apple","pears"];
console.log(fruits.indexOf("pears"));

在这里插入图片描述

对象

创建对象,为对象创建新属性

const person = {
	firstName: "Vincent",
	age: 26,
	hobbies: ["games","movies"],
	address: {
		street: "six road",
		city: "xian",
		state: "MA"
	}
};
console.log(person);

在这里插入图片描述

person.email = "123@163.com"
console.log(person.email);

在这里插入图片描述

对象值存到同名常(变)量中

const person = {
	firstName: "Vincent",
	age: 26,
	hobbies: ["games","movies"],
	address: {
		street: "six road",
		city: "xian",
		state: "MA"
	}
};
const {firstName,age} = person;
console.log(firstName);

在这里插入图片描述

对象数组和JSON

创建对象数组

const todos = [
{
	id : 1,
	text: "take out trash",
	isComplete: true
},
{
	id : 2,
	text: "meeting with boss",
	isComplete: false
},
{
	id : 3,
	text: "take out",
	isComplete: true
}
];
console.log(todos);

在这里插入图片描述

对象数组取值

const todos = [
	{
		id : 1,
		text: "take out trash",
		isComplete: true
	},
	{
		id : 2,
		text: "meeting with boss",
		isComplete: false
	},
	{
		id : 3,
		text: "take out",
		isComplete: true
	}
];
		console.log(todos[1].text);

在这里插入图片描述

对象数组转JSON数据

const todoJSON = JSON.stringify(todos)
console.log(todos);

在这里插入图片描述

if条件语句

const x = 11;
const y = 2;
if(x === 10 || y>1){
	console.log("x is 10");
}else if(x > 10){
	console.log("x > 10")
}else{
	console.log("x is not 10")
}

在这里插入图片描述

三目运算符

const x = 1;
const color = x>10 ? "red" :"blue";
console.log(color);

在这里插入图片描述

switch

注意要带break

const color = "yellow";
switch (color){
	case "red":
		console.log("color is red");
		break;
	case "bule":
		console.log("color is bule");
		break;
	default:
		console.log("color is not found");
}

在这里插入图片描述

for与while

for与while遍历对象数组

const todos = [
	{
		id : 1,
		text: "take out trash",
		isComplete: true
	},
	{
		id : 2,
		text: "meeting with boss",
		isComplete: false
	},
	{
		id : 3,
		text: "take out",
		isComplete: true
	}
];
for(let i=0; i<todos.length; i++){
	console.log(`x值为:${i}`);
	console.log(todos[i]);
}
let y = 0;
while(y<todos.length){
	console.log(`y值为:${y}`);
	console.log(todos[y]);
	y++;
}

在这里插入图片描述

for循环另一种写法(let 变量名 of 对象)

for(let todo of todos){
	console.log(todo.text);
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值