js学习笔记(一) JavaScript

本系列笔记为JavaScript的学习笔记,主要包括一些基础学习内容和链接

基本输出

可以利用console.log()作为输出工具

console.log();`  //用于输出信息
//注释是C语风格的// /*...*/
//分号分开语言,一行多句
//JavaScript 中的 ASI 机制,>换行符<允许我们省略分号。
console.log('JavaScript');
console.log(33.7);
console.log(true);
console.log(null);
console.log('hello world');
console.log('this is float',59.0)   //可以,衔接输出
console.log(2+3)        //可以直接进行+-*/计算

属性

包括字符串、数字等实例都有自己的内置属性和相应的方法。
手册

console.log('hello'.length); //字符串长度属性
5
console.log('Hello'.toUpperCase()); // 'HELLO' console.log('Hey'.startsWith('H')); // true
console.log('Codecademy'.toUpperCase());    //CODECADEY
console.log('   Romeve whitespace  '.trim()); //去掉空格

不需要实例化就可运行的
数学库,num库

//使用例子——数学库
console.log(Math.random()); // random number between 0 and 1
console.log(Math.floor(Math.random()*100))
//取整数,同时将随机数范围扩大到100
Math.floor()
Math.ceil() //向下,向上取整
console.log(Number.isInteger(2017)) //true

·变量

变量分为常量和变量:
变量需要关键字const注释(不能重复赋值)
重复赋值的let注释
变量需要关键字var注释

const location = 'New York City';
let latitude = 40.7;
let inNorthernHemisphere = true;

console.log(location);console.log(latitude);console.log(inNorthernHemisphere)

const entree = 'Enchiladas'
const price = 12
console.log(entree)
console.log(price)

//const entree = 'Tacos' //重复赋值错误
let changeMe = true;
changeMe = false;   //let 可变的变量

let valueless   //没有定义输出 undefined
console.log(valueless)

//运算符
let a = 4
a +=1 // 加1为5
a -=1 // 减1为4
a *=2 // 乘2为8
a /=3 // 除法
a %=3 // 取余数 
a++   // 自增为5
a--   // 自减为3
//注意:++、--先后不一样
x=a++   //x=4,++在后后运算
x=++a   //x=5,++在前先运算

//可以用+号链接字符串
let c = 'console is '+'better tool '
console.log(c+'very good')
//同时可用反引号``和$来输出字符串(${myVariable}) $用来调用变量,``反引号扩住的是一条命令(是一个变量),返回执行结果。
//linux 中:
//val=`expr 2 + 2`
//echo "两数之和为 : $val"
//$() 方法是在DOM中使用过于频繁的 //document.getElementById() 方法的一个便利的简写
console.log(`${c} is very good`)

·流控制

js中的流控制与c中的一样,if/else语句同时使用{}来包含层次关系。

let variableOne = 'I Exist!';
if (variableOne) {
// This code will run because variableOne contains a truthy value.
} else {
// This code will not run because the first block ran.
}

·比较算符
<> <= >=输出trueor false
choose*
==and !=等于和不等于算符(1==‘1’)
===and !==绝对等于和不绝对等于算符,三个(值和类型都比较)
·逻辑算符:与&&||!


·条件语句

一个和少数条件的时候用

if/else if /else


if/else
if/else if()/else more conditions

if(conditon1){
    do some code
} else if(condition i){
    do some code
} else{
  nothing matched
}

多个条件的时候用

switch


switch(*condition*)需要放入条件参数,在内部匹配
同时需要在每一个条件写入break,不像if可以自动结束,不写break就会执行所有case下的代码。
switch(condition) {
....case 'condition i':
---.---.do some code
---.---.break
----case...:
    ...
    break
....default:
    some cde
    break
}

三元运算符,一条语句两种输出x ? x : x
condition(ture/false)?if true run here : if false run here
condition ? true run : false run


tips
1.js如果分行写的话可以省略分号,但很多函数叠加就需要加分号避免错误(初学者建议养成分号习惯)
2.·为零的bools:
falseand0 and -0
"" and '' (empty strings)
nullandundefined
NaN (Not a Number)
document.all (something you will rarely encounter)