ITA-JS

  • JavaScript 对大小写是敏感的。
  • JavaScript是一种弱类型动态语言,这意味着无需事先声明变量的类型。
  • HTML 输出流中使用 document.write,相当于添加在原有html代码中添加一串html代码。而如果在文档加载后使用(如使用函数),会覆盖整个文档。
  • 在 JavaScript 中,用分号来结束语句是可选的。
  • 文本字符串中使用反斜杠对代码行进行换行。
document.write("你好 \
世界!");
  • 向变量赋的值是数值时,不要使用引号。如果用引号包围数值,该值会被作为文本来处理。
  • 未使用值来声明的变量,其值实际上是 undefined。
  • 如果重新声明 JavaScript 变量,该变量的值不会丢失:在以下两条语句执行后,变量 carname 的值依然是 "Volvo":

var carname="Volvo";
var carname;
  •  js 规定的NaN 不等于NaN
  • isNaN() 检测是否是非数值型。
  • 双引号的字符串与单引号的字符串完全相同。但是,以双引号开头的字符串必须以双引号结尾,并且以单引号开头的字符串必须以单引号结尾。
  • 数组中的每个条目都可以保存任何类型的数据
  • JavaScript 只有声明的变量会提升,初始化的不会。
<body>
<p id="demo"></p>
<script>
var x = 5; // 初始化 x
elem = document.getElementById("demo"); // 查找元素 
elem.innerHTML = "x 为:" + x + ",y 为:" + y;           // 显示 5 和 undefined
var y = 7; // 初始化 y
</script>
</body>
  • const 用于声明一个或多个常量,声明时必须进行初始化,且初始化后值不可再修改: 
const PI = 3.141592653589793;
PI = 3.14;      // 报错
PI = PI + 10;   // 报错

 

 


let 和const的区别

const定义常量与使用let 定义的变量相似:

  • 二者都是块级作用域
  • 都不能和它所在作用域内的其他变量或函数拥有相同的名称

两者还有以下两点区别:

  • const声明的常量必须初始化,而let声明的变量不用
  • const 定义常量的值不能通过再赋值修改,也不能再次声明。而 let 定义的变量值可以修改。

const 的本质: const 定义的变量并非常量,并非不可变,它定义了一个常量引用一个值。使用 const 定义的对象或者数组,其实是可变的。 


重置变量

在相同的作用域或块级作用域中,不能使用 let 关键字来重置 var 关键字声明的变量:

var x = 2;       // 合法
let x = 3;       // 不合法

{
    var x = 4;   // 合法
    let x = 5   // 不合法
}

在相同的作用域或块级作用域中,不能使用 let 关键字来重置 let 关键字声明的变量:

let x = 2;       // 合法
let x = 3;       // 不合法

{
    let x = 4;   // 合法
    let x = 5;   // 不合法
}

在相同的作用域或块级作用域中,不能使用 var 关键字来重置 let 关键字声明的变量:

let x = 2;       // 合法
var x = 3;       // 不合法

{
    let x = 4;   // 合法
    var x = 5;   // 不合法
}

let 关键字在不同作用域,或不同块级作用域中是可以重新声明赋值的:

let x = 2;       // 合法

{
    let x = 3;   // 合法
}

{
    let x = 4;   // 合法
}

JavaScript数据类型

ES5的的数据类型(6种)

  • Number -基本类型 (包含NaN)
  • String -基本类型
  • Boolean -基本类型 (有两个字面值,分别是true、false。true不一定等于1,false不一定等于0。)
  • undefined -基本类型
  • object -引用类型(Data、function、Array)
  • Null -基本类型

ES6 中新增了(1种)

  •  Symbol

判断数据类型

typeof

  • 'undefined' - undefined;
  • 'boolean' - Boolean;
  • 'string' - String;
  • 'number' - Number;
  • 'object' - Object or null
  • function - Function
var message = 'some string'; 
alert(typeof message);

用 typeof 检测 null 返回是object。 

typeof 一个没有值的变量会返回 undefined

typeof "John"                 // 返回 string
typeof 3.14                   // 返回 number
typeof NaN                    // 返回 number
typeof false                  // 返回 boolean
typeof [1,2,3,4]              // 返回 object
typeof {name:'John', age:34}  // 返回 object
typeof new Date()             // 返回 object
typeof function () {}         // 返回 function
typeof myCar                  // 返回 undefined (如果 myCar 没有声明)
typeof null                   // 返回 object 

检测数组类型   

  • instanceof操作符

  • 对象的 constructor 属性

  •  Array.isArray( )      

var x=[1,2];
console.log(x instanceof Array);
console.log(x.constructor==Array);
console.log(Array.isArray(x));

null 和 undefined 的区别

  •     Null 只有一个值,是 null。不存在的对象,表示一个空对象指针。
  •     Undefined 只有一个值,是undefined。没有初始化。undefined 是从 null 中派生出来的。
  •     简单理解就是:undefined 是没有定义的,null 是定义了但是为空。 
  • null 和 undefined 的值相等,但类型不等:

    typeof undefined             // undefined
    typeof null                  // object
    null === undefined           // false
    null == undefined            // true


== 和 === 的区别

  • == 表示相同。

            比较的是物理地址,相当于比较两个对象的 hashCode ,肯定不相等的。

            类型不同,值也可能相等。

  • === 表示严格相同。

类型不同直接就是 false。


访问对象属性的两种方法

  • object.property
  • object["property"]. 

JavaScript 显示数据

  • 使用 window.alert() 弹出警告框。
  • 使用 document.write() 方法将内容写到 HTML 文档中。
  • 使用 innerHTML 写入到 HTML 元素。
  • 使用 console.log() 写入到浏览器的控制台。

JavaScript 语句标识符

JavaScript 语句通常以一个 语句标识符 为开始,并执行该语句。

语句描述
break用于跳出循环。
catch语句块,在 try 语句块执行出错时执行 catch 语句块。
continue跳过循环中的一个迭代。
do ... while执行一个语句块,在条件语句为 true 时继续执行该语句块。
for在条件语句为 true 时,可以将代码块执行指定的次数。
for ... in用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作)。
return退出函数
switch用于基于不同的条件来执行不同的动作。
throw抛出(生成)错误 。
try实现错误处理,与 catch 一同使用。
var声明一个变量。
while当条件语句为 true 时,执行语句块。

String的数据类型包含一些特殊的字符文字,也称为转义字符

  • \n: Linefeed 换行
  • \t: Tab 标签
  • \b: Backspace 空格键
  • \r: Carriage return 回车
  • \\: Slash(\)斜线\
  • \': Single quote(')单引号
  • \": Double quotes(") 双引号

String 常用方法

  • concat() -将一个或多个字符串与原始字符串连接起来以形成并返回新字符串。方法不影响原始字符串
var hello = "Hello, ";
console.log(hello.concat("tws", " have a nice day.")); // Hello, tws have a nice day.
console.log(hello); // Hello, 
  • includes()- 确定一个字符串是否包含在另一个字符串中,返回true或false。
  • substr(字符提取的开始位置字符的长度)-将字符从指定位置返回到字符串中的另一个指定位置
var str = 'abcdefghij';
str.substr(0,3); // 'abc'
str.substr(3,3); // 'def'
str.substr(3); // 'defghij'
  • substring(字符提取的起始位置字符提取的结束位置(可选))-返回从索引起始位置到索引结束位置的字符串子集,或者从索引起始位置到字符串结尾的子集。
var str = 'abcdefghij';
str.substring(0,3); // 'abc'
str.substring(3,3); // ''  ( There is no character between 3 to 3.)
str.substring(3); // 'defghij'
str.substring(2,3); // 'c'
方法描述
charAt()返回指定索引位置的字符
charCodeAt()返回指定索引位置字符的 Unicode 值
concat()连接两个或多个字符串,返回连接后的字符串
fromCharCode()将 Unicode 转换为字符串
indexOf()返回字符串中检索指定字符第一次出现的位置
lastIndexOf()返回字符串中检索指定字符最后一次出现的位置
localeCompare()用本地特定的顺序来比较两个字符串
match()找到一个或多个正则表达式的匹配
replace()替换与正则表达式匹配的子串
search()检索与正则表达式相匹配的值
slice()提取字符串的片断,并在新的字符串中返回被提取的部分
split()把字符串分割为子字符串数组
substr()从起始索引号提取字符串中指定数目的字符
substring()提取字符串中两个指定的索引号之间的字符
toLocaleLowerCase()根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLocaleUpperCase()根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLowerCase()把字符串转换为小写
toString()返回字符串对象值
toUpperCase()把字符串转换为大写
trim()移除字符串首尾空白
valueOf()返回某个字符串对象的原始值

数组

创建方式

第一种 :

var fruits = new Array ();
var fruits = new Array (10);

第二种:

var fruits = ['apple', 'pear', 'peach']; // Create an array containing three strings
var names = []; // Create an empty array
var values = [1, 2, ,]; // 别这样!它将创建一个包含2项或4项的数组。

常用方法

  • push():在数组末尾添加一个新元素。
var fruits = ['apple', 'pear', 'peach'];
fruits.push('banana');
console.log(fruits); // ['apple', 'pear', 'peach', 'banana']
  • pop():从数组末尾删除一个元素。

var fruits = ['apple', 'pear', 'peach'];
fruits.pop();
console.log(fruits); // ['apple', 'pear']
  • unshift():在数组的前面添加一个新元素。

var fruits = ['apple', 'pear', 'peach'];
fruits.unshift('banana');
console.log(fruits); // ['banana', 'apple', 'pear', 'peach']
  •  shift():删除数组前面的元素。
var fruits = ['apple', 'pear', 'peach'];
fruits.shift();
console.log(fruits); // ['pear', 'peach']
  • indexOf():查找数组中某些数据项的索引值。

var fruits = ['apple', 'pear', 'peach'];
console.log(fruits.indexOf('pear')); // 1 
  •  数组中的length属性很有特色。它不是只读的。因此,我们还可以通过设置此属性来删除数组末尾的数据项。例如:
var fruits = ['apple', 'pear', 'peach'];
fruits.length = 2;
console.log(fruits[2]); // undefined – It has the equivalent effect of deleting the third item from the array. 

break和continue的区别

  • break 语句用于跳出循环。
  • continue 用于跳过循环中的一个迭代。
  • continue 语句(带有或不带标签引用)只能用在循环中。
  • break 语句(不带标签引用),只能用在循环或 switch 中。
  • 通过标签引用,break 语句可用于跳出任何 JavaScript 代码块:

Which writing method aiming at loading the external document script.js in current path on a web page is incorrect?

  • <link href="script.js" />
  • <script src="script.js"></script>
  • <script language=”javascript” src="script.js">
  • </script><script type="text/javascript" src="script.js"></script>

Which description about loading an external js document is correct?

  • It is not allowed to load an *.js document beyond the website;(不允许在网站之外加载* .js文档;)
  • One benefit of loading document is that you can just manage one piece of codes and need not edit every HTML document when codes change;(加载文档的好处之一是,您只需管理一段代码,而无需在代码更改时编辑每个HTML文档。)
  • An external *.js document is loaded through href attribute of <script> tag in HTML;(外部* .js文档是通过HTML中<script>标签的href属性加载的;)
  • An external *.js document is loaded through src attribute of <script>tag in HTML.(外部* .js文档通过HTML中<script> tag的src属性加载)

Please select the expression whose result is true:

  • null instanceof Object
  • null === undefined
  • null == undefined
  • NaN == NaN

虽然null的类型是object,但是null不具有任何对象的特性;

要比较相等性之前,不能将 null 和 undefined 转换成其他任何值,并且规定null 和 undefined 是相等的。

null 和 undefined都代表着无效的值;

全等操作 === 在比较相等性的时候,不会主动转换分项的数据类型,而两者又不属于同一种类型;

undefined 表示一个变量自然的、最原始的状态值,而 null 则表示一个变量被人为的设置为空对象,而不是原始状态。所以,在实际使用过程中,为了保证变量所代表的语义,不要对一个变量显式的赋值 undefined,当需要释放一个对象时,直接赋值为 null 即可。


var time;
console.log( typeof(time) );

What’s the output of the above codes in console? 

  • undefined
  • null
  • string
  • number

  var data = typeof(123);

Then, what’s the return value of typeof(data)?

  • 'number'
  • undefined
  • 'string'
  • 'Function'

How many kinds of basic data type are there in JavaScript?(ECMAScript 2015)

  • 7
  • 8
  • 6
  • 5

弹出“未定义”的原因是,当首先执行alter(x)时,局部变量x尚未初始化 。

<script type="text/javascript">
    var x = 1;
    function rain(){
        alert( x );        // 'undefined', instead of 1 will pop up.
        var x = 'rain-man';
        alert( x );        // 'rain-man' will pop up.
    }
    rain();
</script> 

for(let i = 0; i < 10; i++){
  //Please image there is code between “{” and “}” but it cannot operate i. 
}
console.log(i)

What’s the output of the above codes in console? (Two spaces represent linefeed). 

  • 0
  • 9
  • 10
  • Uncaught ReferenceError: i is not defined

i用let声明,是局部变量


for(var i = 0; i < 10; i++){
  //Please image there is code between “{” and “}” but it cannot operate i. 
}
console.log(i)

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • 0
  • 9
  • 10
  • Uncaught ReferenceError: i is not defined

for(var i = 0; i < 3; i++){
  var result = [];
  result.push(i);
}
console.log(result);

What’s the printing result of the last line of the above codes? 

  • [ 0, 1, 2 ]
  • [ 0, 1, 2, 3 ]
  • [ 2 ]
  • [ 3 ]

result是在for循环声明并初始化,所以每次循环都会赋值为空。


for(var i = 0; i < 3; i++){
  let result = [];
  result.push(i);
}
console.log(result);

What’s the printing result of the last line of the above codes? 

  • [ 0, 1, 2 ]
  • [ 0, 1, 2, 3 ]
  • [ 2 ]
  • Uncaught ReferenceError: result is not defined

result是用let声明,是布局变量,外面访问不到。


var result = [];
for(var i = 0; i < 3; i++){
    result.push(i);
    console.log(result);
}

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • [ 0, 1, 2 ]
  • [ 0 ] [ 0, 1 ] [ 0, 1, 2 ]
  • [ 2 ]
  • Uncaught ReferenceError: result is not defined

var x = 0;
function fun(){
    console.log(x);
    var x = 1;
    x++;
}

fun();

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • 0
  • 1
  • undefined
  • null

虽然在外面x为全局变量,但是在function里面输出x之前没有声明,所以是 undefined。


var x = 0;
function fun(){
    console.log(x);
    let x = 1;
    x++;
}

fun();

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • 0
  • 1
  • undefined
  • Uncaught ReferenceError。(无法访问未初始化的变量。)

如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。


var str="Hello word!";
str.replace("word","tlt");
console.log(str)

What’s the output of the above codes in console? 

  • Hello word!
  • Hello word tlt
  • Hello tlt
  • {Uncaught SyntaxError=missing ) after argument list}

replace方法不影响原字符串


Does a template string support linefeed?(模板字符串是否支持换行?)

  • Yes, it does.
  • No, it doesn’t.

Which item correctly calls a variable with template string?(哪一项正确调用带有模板字符串的变量?)

  • `Hello {$name}`
  • `Hello {{name}}`
  • `Hello $name
  • `Hello ${name}`

console.log("There are "+3+2+" students in our class")

What’s the output of the above codes in console? 

  • There are 32 students in our class
  • There are 5 students in our class

What’s the calculation result of

  (7 - 1) / (4 + 6) % 4; // (Note) It is allowed to change order of operations. 
```?
  • 0
  • 1
  • 0.6
  • NaN

6/10%4    一步一步来,注意顺序和细节。


var x = 2, y = 5;
x += ++y;

Which is the general writing form of the code x += ++y? 

  • x = x+y+1
  • x = x+y
  • x = x+y+1; y = y+1
  • y = y+1; x = x+y(先自增,再和x相加)

var x = 2, y = 5;
x += y++;

Which is the general writing form of the code x += y++ ? 

  • x = x+y+1; y = y+1
  • x = x+y+1
  • x = x+y; y = y+1(先相加,后自增)
  • y = y+1; x = x+y

var num1 = 3, num2 = 7;
num1 = num2;
num2 = num1;
console.log( `num1 = ${num1}; num2 = ${num2};` );

What’s the output of the above codes in console? 

  • num1 = 7; num2 = 3;
  • num1 = 3; num2 = 7;
  • num1 = 3; num2 = 3;
  • num1 = 7; num2 = 7;

var num1 = 5;
console.log(num1);
console.log(num2);
var num2 = 3;
console.log(num1+num2);

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • 5 Uncaught ReferenceError: num2 is not defined
  • undefined undefined 8
  • 5 undefined 8
  • 5 3 8

let num1 = 5;
console.log(num1);
console.log(num2);
let num2 = 3;
console.log(num1+num2);

What’s the output of the above codes in console? (Two spaces represent linefeed.) 

  • 5 Uncaught ReferenceError: num2 is not defined
  • undefined undefined 8
  • 5 undefined 8
  • 5 3 8

console.log(8*2 => 16);

What’s the output of the above codes in console? 

  • true
  • false
  • 16
  • Uncaught SyntaxError: Malformed arrow function parameter list  

大于等于>=  细心的兄der


for( var i = 0; i < 10; i++){
    if(i < 7){
        continue;
    }
    console.log( i );
}

What’s the output of the above codes in console? (Space represents linefeed.) 

  • 7 8 9
  • 0 1 2 3 4 5 6
  • 8 9
  • 0 1 2 3 4 5 6 7

let i = 0;
while(i<10){
    i++;
    if(i%2 == 0){
        console.log(i);
    }
}

 What’s the output of the above codes in console? (Space represents linefeed.)

 

  • 2 4 6 8
  • 0 2 4 6 8
  • 2 4 6 8 10
  • 0 2 4 6 8 10

Which description about break is incorrect?

  • break is used to exit a loop.(break用于退出循环。)
  • break is used to exit current function.(break用于退出当前功能。)
  • break is used to exit switch statement.(break用于退出switch语句。)
  • break can not break out of an if statement(break不能脱离if语句)

Which description about continue is correct?

  • continue can be used in loop statements only.(continue只能在循环语句中使用。)
  • Uses of continue are the same as those of break.(continue的用法与break的用法相同。)
  • continue is used to exit a loop.(continue用于退出循环。)
  • continue means continuous execution of the subsequent code(continue表示连续执行后续代码。)

for (let i=0; i<10; i++){
    if(i%2 == 0){
        console.log(i);
    }
}

What’s the output of the above codes in console? (Space represents linefeed.) 

  • 2 4 6 8
  • 0 2 4 6 8
  • 2 4 6 8 10
  • 0 0 0 0 0

function compare(a, b){
  let result;
  function result(a, b){
      result = a>b ? "y" : "x";
      return result;
  }
  var result = result(a, b);//出错
  var string = `${a} is ${result} than ${b}`;
  return string;
}
console.log( compare(5,5) );

What’s the output of the above codes in console?

  • 5 is y than 5
  • 5 is x than 5
  • 5 is undefined than 3
  • Uncaught SyntaxError: Identifier 'result' has already been declared

function compare(a, b){
  var result;
  function result(a, b){
      result = a>b ? "y" : "x";
      return result;
  }
  var result = result(a, b);
  var string = `${a} is ${result} than ${b}`;
  return string;
}
console.log( compare(5,5) );

What’s the output of the above codes in console? 

  • 5 is y than 5
  • 5 is x than 5
  • 5 is undefined than 3
  • Uncaught SyntaxError: Identifier 'result' has already been declared

Which statement will have an error during running?

  • var obj = { };
  • var obj = ( );
  • var obj = [ ];
  • var obj = / /;

let phone = {zone : null, number : null};
let phone_number = phone['zone'] + phone.number;
console.log(phone_number);

What’s the output of the above codes after execution in console?  

  • 0
  • undefined
  • NaN
  • nullnull

let phone = {zone : '010', number : '12345678'};
let phone_number = phone['name'] + phone.age;
console.log(phone_number);

What’s the output of the above codes after execution in console?  

  • 01012345678
  • undefined
  • NaN
  • undefinedundefined

let phone = {zone : '010', number : '12345678'};
let phone_number = phone['name'] + phone.number;
console.log(phone_number);

What’s the output of the above codes after execution in console?  

  • 01012345678
  • undefined
  • 12345678
  • undefined12345678

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值