JavaScript学习笔记 – 1 – 数据类型和变量

§ 0.1 – JavaScript中的变量种类


  • Number类型,代表整数和小数;
  • String类型,代表字符串;
  • Boolean类型,代表二元逻辑真伪;

§ 0.2 – JavaScript中的返回值种类


  • Undefined类型,代表未赋值(未知类型)的变量;
  • Null类型,代表已经赋值但是值为空(empty)的变量;
  • NaN(Not a Number)类型,表示“非数字”,通常返回NaN表示数字运算存在错误;

§ 1.0 – Number类型变量


1.1 变量声明
// Variable Types - Number
var number = 1;
console.log(number);

§ 2.0 – String类型变量


2.1 变量声明
// Variable Types - String
var firstString = "This is a String.";
var secondString = "This is another String.";
2.2 字符串连接
// Concatenation of String
console.log(firstString + " " + secondString); 
// results "This is a String. This is another String."
2.3 字符串索引
// Index of String
console.log(firstString[0]); 
// results "T"
2.4 字符串中的特殊符号转义
// Escape String/special Char with "\"
console.log("\"C:\\\\Desktop\\My Documents\\Roster\\names.txt\"");

// Newline with "\n"
console.log("Nice to meet you!\nNice to meet you, too!");

// New tab with "\t"
console.log("Nice to meet you!\tNice to meet you, too!");
2.5 字符串比较
/*
   Compare of Strings
   ------------------
   When checking if a string is "greater than" or "less than" another string,
   JavaScript compares individual characters using a numerical value.
   Each character is assigned a numerical value that essentially corresponds 
   to the character's location in an ASCII
   Lowercase letters have higher numerical values in the ASCII table than uppercase letters.
 */
console.log("Green" > "Blue"); // returns true
console.log("Green" > "green"); // returns false
console.log("green" > "Green"); // returns true

 

发表评论