v1.11.4
For your convenience, we have collected the basics of JavaScript in a cheatsheet.
Below, some questions to check that the JavaScript syntax is read and understood.
How is an external JavaScript loaded into a document
<link rel=”javascript” href=”scriptfile.js”>
<script src=”scriptfile.js”></script>
<script href=”scriptfile.js”></script>
<import rel=”javascript” src=”scriptfile.js”>
Where in a document can you include JavaScript?
Inside the head element
Inside the body element
Inside either head or body element
How do you specify a block-scope variable tmp with value 2?
tmp
int tmp = 2;
Number tmp = 2;
tmp = 2;
let tmp = 2;
var tmp = 2;
How do you specify a block-scope constant tmp with value 2 that will not be changed?
const tmp = 2;
let tmp =2;
What is a block?
code rows that get auto-indented similarly
statements grouped by some entity, e.g., function
something within curly brackets
the inner content of a control structure such as for or while
for
while
What is the term for raising var declarations to have a function-scope visibility even if the var variables were defined inside an inner-block of a function?
var
Which of the following code snippets are correct and not causing any errors?
// a function sayHi() {console.log('hi!')} sayHi()
// b sayHi() function sayHi() {console.log('hi!')}
// c var sayHi = function sayHi() {console.log('hi!')} sayHi()
// d sayHi() var sayHi = function sayHi() {console.log('hi!')}
// e (function sayHi() { console.log('hi!') })()
a is OK
b is OK
c is OK
d is OK
e is OK
(function sayHi() { console.log('hi!') })()
() in the end of the function above implies that ..
()
e is a function
e does not take any parameters
e is self-invoked
Data types can be primitives or objects
primitives have to be instantiated with a keyword new
new
built-in objects are often instantiated with a keyword new
everything instantiated with new has to be deleted expliticly
primitives include int, float, string, and object
== comparison
==
causes type coercions
prints false when console.log(1 == ‘1’) is called
is called a strict comparison
=== comparison
===
prints false when console.log(1 === ‘1’) is called
How can you specify an array with initial values in JavaScript?
let arr = [1, 2, 3];
let arr = {1, 2, 3};
const arr = (1, 2, 3);
Define an empty array with two characters only (i.e., the max-length of the definition is two chars):
let array1 = ...
Define an empty object (used as an associative array or map) with two characters only:
let object1 = ...
Functions in JavaScript
are first-class citizens
print object if typeof() is called having a function as an argument
object
typeof()
are objects
can be assigned to a variable
If a following JavaScript Snippet is executed, what will happen?
function examineArguments(a,b,c){ console.log(a,b,c); } const a=1; const b=2; const c=3; const d=4; const e=5; const f=6; examineArguments(a,b,c,d,e,f);
a runtime error will be thrown
the program will print “1 2 3”
function cannot be found since the signature differs from what is expected
Following JavaScript Snippet is executed, what will happen:
function examineArguments(a, b ,c){ console.log(a, b, c); } const a = 1; const b = 2; const c = 3; examineArguments(a, b);
the program will print “1 2”
the program will print “1 2 undefined”
Which of the following does specify a function that will return a number?
function myFunction() { return 3; }
public Number function myFunction() { return 3; }
var myFunction = function() { return 3; }
Number function myFunction() { return 3; }
function addTenStupid(num) { for(i=0; i < 10; i++) { num += 1; } return num; } function addThousandStupid(num) { for(i=0; i < 100; i++) { num += addTenStupid(0); } return num; } console.log(addTenStupid(0)); console.log(addTenStupid(5)); console.log(addThousandStupid(1));
addTenStupid(0), first console log, prints 10
addTenStupid(5), second console log, prints 15
addThousandStupid(1) prints nothing
this example is to demonstrate that global variables are evil