JavaScript 基础(一)

这篇博客介绍了JavaScript的基础知识,包括变量、赋值、数据类型、运算符、控制结构、字符串和模板字符串的使用。此外,还讨论了类型转换、条件判断以及代码风格规范,如变量命名和作用域。示例代码展示了如何在实际场景中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 变量和赋值

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值