Javascript:02基础部分1.3

013 let, const and var

本小节主要讲了声明变量的三种方式

视频加载中...


声明变量有三种方式let、const、var。let和const是在ES6中新引入。var是旧方法。

通过let声明age变量,给age变量赋值30,然后改变age变量的值为31。

通过const声明birthYear变量,此变量的值1991不允许被修改。如果将birthYear的值赋值为1990,则控制台可以看到错误提示信息。

通过const声明的变量不能为空,必须为该变量赋值。声明const变量job未赋值,在控制台可以看到错误提示信息“const声明未初始化”。

通过var声明变量的方式,不要使用,这里只是了解一下它。

类似通过let声明变量一样。但是,在控制台上没有任何提示信息。var和Let的区别,后续课程继续详细说明。

如果没有强制声明变量,直接给出变量名,然后赋值。看上去,即使没有使用let或const声明变量lastName,但是它好像也有效了。这样是错误的,因为实际上没有在当前范围内创建变量,而是创建了全局属性,一定区分开这种情况。


014 Basic Operators

本节主要讲基本运算符。

算术运算符:加, 减, 乘, 除

赋值运算符:=, +=, *=,++,--

比较运算符:>, <, >=, <=

视频加载中...


运算符:-,示例如下:

const ageJonas = 2037 - 1991;
console.log(ageJones);

控制台输出结果46。

运算符:-,示例如下:

const ageJonas = 2037 - 1991;
const ageSarah = 2037 - 2018;
console.log(ageJones, ageSarah);

控制台输出结果46 19。

注意,console.log输出多个结果的用法。

const now = 2037;
const ageJonas = now - 1991;
const ageSarah = now - 2018;
console.log(ageJones, ageSarah);

控制台输出结果46 19。

代码中2037重复出现,可以通过声明变量来替换。

运算符:*,示例如下:

console.log(ageJonas * 2, ageJonas / 10, 2 ** 3);
// 2 ** 3 means 2 to the power of 3 = 2 * 2 * 2

控制台输出92 4.6 8

运算符:+,用于连接两个字符串示例:

const firstName = 'Jonas';
const lastName = 'Schmedtmann';
console.log(firstName + lastName);

在控制台输出JonasSchmedtmann

运算符:+,用于连接两个字符串带空格示例:

const firstName = 'Jonas';
const lastName = 'Schmedtmann';
console.log(firstName + ' ' +  lastName);
在控制台输出Jonas Schmedtmann

赋值运算符:=

let x = 10 + 5;
console.log(x);

在控制台输出15

赋值运算符:+=

let x = 10 + 5; // 15
x += 10; // x = x + 10 = 25

console.log(x);

在控制台输出25

赋值运算符:*=

let x = 10 + 5; // 15
x += 10; // x = x + 10 = 25
x *= 4; // x = x * 4 = 100
console.log(x);

在控制台输出100

赋值运算符:++

let x = 10 + 5; // 15
x += 10; // x = x + 10 = 25
x *= 4; // x = x * 4 = 100
x++; // x = x + 1
console.log(x);

在控制台输出101

赋值运算符:--

let x = 10 + 5; // 15
x += 10; // x = x + 10 = 25
x *= 4; // x = x * 4 = 100
x--;
x--;
console.log(x);

在控制台输出99

比较运算符:>

console.log(ageJonas > ageSarah);

控制台输出true

比较运算符:>=

console.log(ageJonas > ageSarah);
console.log(ageSarah >= 18);

控制台输出true

定义的const isFullAge变量的类型为boolean类型。

const isFullAge = ageSarah >= 18;
console.log(now - 1991 > now - 2018);

控制台输出true

从运算符的优先级来看,先计算,后比较。


015 Operator Precedence

本节课程主要讲操作符的优先级。

视频加载中...

运算符优先级表格,查看网址:

杩愮畻绗︿紭鍏堢骇 - JavaScript | MDNMDN Web DocsMDN logoMozilla logo

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table

通过表格,可以知道减号(-)运算符优先级高于大于号(>)运算符,所以

const now = 2037;
const ageJonas = now - 1991;
const ageSarah = now - 2018;
console.log(now - 1991 > now - 2018);

在控制台输出布尔值为true


console.log(25 - 10 - 5);

在控制台输出10

减号(-)运算符运算顺序是从左到右。


let x, y;
x = y = 25 - 10 - 5;
console.log(x, y);

在控制台输出10 10

赋值(=)运算符的优先级相对来说最低,并且赋值运算是从右向左进行。


const averageAge = ageJonas + ageSarah / 2
console.log(ageJonas, ageSarah, averageAge);

在控制台输出46 19 55.5

根据优先级表格,除法优先级高于加法,所以计算年龄的平均值结果不符合预期。


const averageAge = (ageJonas + ageSarah) / 2
console.log(ageJonas, ageSarah, averageAge);

在控制台输出46 19 32.5

根据优先级表格,除法优先级高于加法,使用括号将加法计算括起来,括号优先级高于除法,所以这次计算年龄的平均值结果正确。


016 Coding Challenge #1

视频加载中...

Mark and John are trying to compare their BMI (Body Mass Index), which is calculated using the formula: BMI = mass / height ** 2 = mass / (height * height) (mass in kg and height in meter).

Your tasks:

1. Store Mark's and John's mass and height in variables

2. Calculate both their BMIs using the formula (you can even implement both versions)

3. Create a Boolean variable 'markHigherBMI' containing information about whether Mark has a higher BMI than John.

Test data:

Data 1: Marks weights 78kg and is 1.69m tall. John weights 92kg and is 1.95m tall.

Data 2: Marks weights 95kg and is 1.88m tall. John weights 85kg and is 1.76m tall.


const massMark = 78;
const heightMark = 1.69;
const massJohn = 92;
const heightJohn = 1.95;

const BMIMark = massMark / heightMark ** 2;
const BMIJohn = massJohn / (heightJohn * heightJohn);
console.log(BMIMark, BMIJohn);

在控制台输出27.309968138370508 24.194608809993426


const massMark = 78;
const heightMark = 1.69;
const massJohn = 92;
const heightJohn = 1.95;

const BMIMark = massMark / heightMark ** 2;
const BMIJohn = massJohn / (heightJohn * heightJohn);
const markHigherBMI = BMIMark > BMIJohn;
console.log(BMIMark, BMIJohn,  markHigherBMI);

在控制台输出27.309968138370508 24.194608809993426 true


const massMark = 95;
const heightMark = 1.88;
const massJohn = 85;
const heightJohn = 1.76;

const BMIMark = massMark / heightMark ** 2;
const BMIJohn = massJohn / (heightJohn * heightJohn);
const markHigherBMI = BMIMark > BMIJohn;
console.log(BMIMark, BMIJohn,  markHigherBMI);

在控制台输出26.87867813490267 27.44059917355372 false

展开阅读全文

页面更新:2024-05-15

标签:减号   除法   赋值   加法   优先级   高于   控制台   变量   表格   声明   基础

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top