JavaScript Flashcards
What is the result of below
“apple”.length
“5”
Remember, index does not matter here. It just count the number of words
Also, .length method cannot be applied in array.
how many words is counted if there is \n inside a string
1 only, not two
in Javascript, what will console.log (0.2+0.4) result in?
0.6000000000000001, the reason is computer in binary digits, 在電腦的角度, 這些小數點是除不盡的. 所以衹要電腦是計算小數點, 都會有不精准的情況, 所以在計算價錢的時候要特別注意
const sentence = “Teck Academy is good”
console.log(sentence.substring(sentence.length-4))
What is the purpose of substring. what will come out
the argument in substring is that what LAST number of index of string the computer will show
what is NaN
“not a number” that’s a bug
how to use math method in Javascipt
console.log(Math.max(1,2,3,4,5))
how to eliminate digitial number or usye the digitial number to round up to interger
Math.floor(1.xxx) = 1
Math.ceiling
Math.round
Before Node.js is invented, where was Javascript run?
only on web brower
when is camel case format used? And how?
We normally name function using the camel case format. The name normally starts with a verb because a function is normally a function on its own.
// Correct function sumOfArray(){ }
// Incorrect function sum_of_array(){ }
// Incorrect function sumofarray(){ }
What is the difference between expression and statement?
The correct answer is: if-else block are statements while ternary operators condition ? true-value: false-value is expression.
what is the symbol of END operator in Boolean. What are OR and NOT
&&, ||, !
comparsion operator >=口號, and !=
大於或等於
can 15% be 0.015 in Javascript?
NO. It does not exist and give error
Uncaught c:\Users\C\Documents\tacky_js_fund\001.js:69
can I declare x = 1, two times
no, the second time just declare “x = 1”
how to add item in array
array.push(XXX), it will go to the last index of the array
how to remove the last item in array
array(/llzlazlease z
two methods that can access the value in object thought key? What are they?
chris = {‘height’ : 180}
chris[‘height’]
OR
chris.height
SPECIAL case in the key of object,
if in the key of object, there is for exmaple,
“living place” or “living-place”
can I type chris.living-place?
then you cant use
chris.living-place, because it just cant, computer will think it is minus sign
can you add a unnecessary comma after the last item in array and object
yes you can, there wont be error, also this is convenient for you to copy and paste paste
in object, what if you want to delete one key with its value?
you just write
delete chris[key]
保持隊形
就算空了也要給個空的array
what is good structure if I want to limit the while loop to the number of array?
while (n< nameList.length) THIS
n = 0 while (n< nameList.length){ if (nameList[n].height > 170){ console.log(nameList[n].name) } n += 1
arrry add and delete things?
// To add a new element to the end of the array arr.push(5);
// To remove a element from the end of the array arr.pop();
// To add a new element to the start of the array arr.unshift(0);
// To remove a element from the start of the array arr.shift();
function return發生多少次
return 只會發生一次, 其他return就會被忽略
what should be the the name of assignement to array
arr
how to loop object>
for in loop
what scope is let and const, what scope is var
let and const are block scope, only effective inside a block, var is function scope, effective everywhere, except in a function
It’s short-coming is it let you define more than once.
for example,
var x = 2 var x = 1
how to see if there is an element in a array?
Example
Check if an array includes “Mango”:
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.includes("Mango") // Returns true
how to set interval?
setIntervel(function(){
}, 1000)
Is this code ok?
let x = 2; let x = 3;
const y = 2; const y = 3;
No, it will say Uncaught
How would you define scope of variable?
Scope determines the accessibility (visibility) of variables.
JavaScript has 3 types of scope:
- Block scope
- Function scope
- Global scope
Before ES6 (2015), what scope does JavaScript only had?
What did ES6 introduced?
- Global Scope
- Function Scope
ES6 introduced “let” and “const”, which provide BLOCK SCOPE.
What is a block scope in JavaScript?
Variables declared inside a { } block cannot be accessed from outside the block:
{ let x = 2; } // x can NOT be used here
What scope if one declares:
var x = 2;
What does it mean
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
What is local scope and what does it mean?
Variables declared within a JavaScript function, become LOCAL to the function.
// code here can NOT use carName
function myFunction() { let carName = "Volvo"; // code here CAN use carName }
// code here can NOT use carName
What is local scope and what does it mean?
Variables declared within a JavaScript function, become LOCAL to the function.
Local variables have Function Scope:
They can only be accessed from within the function.
// code here can NOT use carName
function myFunction() { let carName = "Volvo"; // code here CAN use carName }
// code here can NOT use carName
Since local variables are only recognized inside their functions, can we use the same name in different functions?
Yes, we can use the same name
What happened after a local variable is used in a function?
Local variables are created when a function starts, and deleted when the function is completed.
A variable declared outside a function, becomes __________ ?
GLOBAL
let carName = "Volvo"; // code here can use carName
function myFunction() { // code here can also use carName }