// 变量和赋值
console.log("hello world");
let js = "amazing";
console.log(js);
// if (js == "amazing") alert("amazing");
console.log(40 + 10);
console.log("amzazing");
let firstName = "Octopus";
console.log("FistName:", firstName);
firstName = "Zhangyu";
console.log("FristName:", firstName);
// 建议驼峰命名,变量不能以数字开头,可以包含数字,不能包含特殊字符类似 &, 但是 _, $ 是可以的, 不能用关键字
// 不要以大写字母开头写变量
// 常量全部大写字母
// 变量定义最好有一定的意义,具有可读性
let _function = 27;
let PI = 3.1415;
let myFirstJob = "programmer";
let myCurrentJob = "teacher";
// 数据类型
// 值 object / primitive
// 原始数据类型:
// Number: float point number
// String:字符集合
// Boolean: true / false
// Undefined: 声明变量但是不赋值 let js;
// Null: 空值
// Symbol: 唯一值,无法更该
// BigInt:超过 Number 范围的整数
// Dynamic Typing 动态类型
// 单行注释
/*
多行注释
多行注释
*/
console.log(true);
console.log(null);
console.log(typeof true);
console.log(typeof 123);
console.log(typeof "21");
let secondName = 12;
console.log(typeof secondName);
secondName = "teacher";
console.log(typeof secondName);
// 变量声明
// let: 声明可变变量和未赋值变量
// const: 不可更改的变量,必须有初值
// var: 尽量避免,旧的方式, 具有作用域
let age = 30;
age = 31;
const birthYear = 1991;
var habbit;
habbit = "123";
habbit = 123;
// 不要这样做
lastName = "Octopus";
console.log(lastName);
// 运算符
const now = 2037;
const ageOctopus = now - 1998;
const ageZy = now - 1998;
console.log(ageOctopus, ageZy);
console.log(ageOctopus * ageZy, ageZy / 2, 2 ** 3);
console.log("KKKK" + "JJJJ")
let x = 10 + 5;
x += 10;
x++;
console.log(x);
console.log(2 > 1);
console.log("1555" > "1456");
// 运算符优先级
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
// String
const hello = "Hello" + " " + "World " + 2022 + " " + true;
console.log(hello);
// 模板字符串
const helloTemplate = `${hello}, ${2 - 1}`;
console.log(helloTemplate);
console.log(`"hello world"`);
console.log("hello \n\world");
// if-else 控制结构
// just have fun
const ageNow = 19;
const isOldEnough = age >= 18;
if (isOldEnough) {
console.log("can drive 🚗");
} else {
console.log("can not drive 🚗");
}
// 类型转换
const inputYear = "1999";
console.log(inputYear + 1) // 19991 字符串相加
console.log(Number(1999) + 1) // 2000
console.log(typeof NaN) // number类型, 代表无效数字
console.log(String(1999))
// 类型强制转换
console.log(inputYear + 1) // 19991 字符串相加
console.log("23" - "10") // 13,数字相减
// boolean
console.log(Boolean("octopus"))
console.log(Boolean(undefined))
console.log(Boolean(NaN))
console.log(Boolean("0"))
console.log(Boolean(0))
// == 和 ===
const lenNow = 3;
if (lenNow === 3) { // 强等于
console.log(`lenNow is equal ${lenNow}`)
}
if ("3" == 3) { // 弱等于
console.log(`lenNow is equal ${lenNow}`)
}
const inputNum = prompt("input number: ");
if (inputNum === 666) {
console.log(`guess right string!`);
}
if (Number(inputNum) === 666) {
console.log(`guess right number!`);
}
// != 和 !==
if (Number(inputNum) !== 666) {
console.log(`guess wring number!`);
}
// AND NOT OR
const hasGoodVision = true
const hasDriverLicense = true
console.log(hasDriverLicense && hasDriverLicense)
// switch
const speed = 100
switch (speed) {
case 100:
console.log("too fast");
break;
case 102:
console.log("normal speed");
break;
default:
console.log("not running");
break;
}
// 语句和表达式
// 语句是完整的句子,表达式产生值
// 条件运算符
const color = "blue";
color === "blue" ? console.log("color is blue") : console.log("color is not blue");
const bill = 275;
const tip = bill <= 300 && bill > 50 ? bill * 0.15 : bill * 0.1;
console.log(bill)
JavaScript 基础(一)
最新推荐文章于 2025-12-15 15:15:21 发布
这篇博客介绍了JavaScript的基础知识,包括变量、赋值、数据类型、运算符、控制结构、字符串和模板字符串的使用。此外,还讨论了类型转换、条件判断以及代码风格规范,如变量命名和作用域。示例代码展示了如何在实际场景中应用这些概念。
2516

被折叠的 条评论
为什么被折叠?



