Basics Flashcards

1
Q

How do you add a JS script to a HTML page?

A
<script type="text/javascript">
    // Your code goes here ...
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you include an external JS file in an HTML page?

A
<script src="path/to/filename.js"></script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you change the contents within a HTML element with id=’target’, with a button click?

A
<button tyle='button' onclick=' document.getElementById("target").innerHTML = "Hello World!" '>Click Me!</button>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you change the font size of an element with id=’target’?

A
document.getElementById("target").style.fontSize = "32px";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you hide an element with id=’target’?

A
document.getElementById("target").style.display = "none";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a basic JS function to change the contents of an element with id=’target’.

A
<script>
function myFunction() {
  document.getElementById("target").innerHTML = "Hello World!";
}
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you call an elfunction with a button click?

A
<button type="button" onclick="myFunction()">Try it</button>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you trigger an alert box to pop up?

A
alert('This is an alert!')

or

window.alert('This is also an alert!')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a prompt to get someone’s age

A
let age = prompt("What's your age?")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is duck typing?

A

If it looks like a duck and sounds like a duck it must be a duck!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you find out the type of a variable

A

use the typeof command. For example:

var a = 10;
console.log(typeof a)
>>> number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will ‘typeof null’ return?

A
>>> object

There is a bug in JS that they can’t fix (without breaking a lot of legacy code). It returns object and not null (it is a primitive, not a reference type).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between == and ===?

A

== uses type coercion.
=== compares for equality but without any type coercion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is type coercion

A

Type coercion is where a variable type is change by JS automatically. For example using a number as a boolean.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is block scope?

A

It is anything contained within curly braces {}
This is used a lot, if for example if statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is function scope

A

function scope is anything defined within the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the difference between block and function scope?

A

Variables created within a block scope are available to the parent scope, this is not the case for function scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the heap?

A

A mostly unstructured area of memory where JS objects are stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the stack?

A

it is the JS que, a todo list of code to execute. Each frame in the stack represents a function call in the code being executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do browsers and APIs enhance JS?

A

Browsers and web API’s are not part of the JS language, but provide extensions to what you can do with the language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the task queue?

A

The task queue is part of the browser, and is responsible for managing the stack and the task queue. There is a seperate render queue for the display.

22
Q

What is blocking?

A

Code that is slow, like network requests or image processing, can hold up the processing of frames in the stack and prevent the user from interacting with the browser.

Asynchronous callbacks are the answer to browser blocking.

23
Q

Write a turnery operator statment

A
let food = 'bread';

let breakfast = (y === 'bread') ? 'toast' : 'no toast';
24
Q

What is an IIFE?

A

It’s an ‘Immediately Invoked Function Expression’

Putting a function in braces makes it an expression, and puting an empty set of braces after it tells JS to call or invoke the function immediately

(favNumber = function (num = 3) {

   console.log("my favorite number is " + num);

})();

favNumber(10);

This code will return twice, once with 3 and once with 10.
25
Q

What are the main JS engines?

A
  • V8 - The Chrome engine and nodejs
  • Spidermonkey - Firefox
  • Chakra - Microsoft and there version of node
  • JSC - JavaScriptCore was initially built for webkit and now powers Safari and React Native Applications.

You can play with these engines directly using jsvu at the command line

26
Q

How do JS engines work?

A

All the JS engines follow the same basic process but have different systems for the optimisation.

The source code is fed into the parser, and this creates the abstract syntax tree. This is fed into the interpreter which optimises and executes the code.

V8 has one optimiser, Spidermonkey and Chakra both have two, and JSC has 3 code optimisers!

V8 is written in C++, and the code is open source. Thus, you can expand on its codebase if you want to.

27
Q

How do bitwise operations work?

A

The bitwise operator goes though each column comparing values.

console.log (1 | 2);

will compare 00000001 or 00000010. The result is 00000011, or 3,.
28
Q

What are the logical operators for OR and AND?

A

OR is

||

AND is
&&

The bitwise operators for OR and AND are simply

|
and
&
29
Q

What are the three basic data types in JavaScript?

A

strings, numbers, and booleans

30
Q

How do you declare a variable in JavaScript?

A

You can declare a variable in JavaScript using the “var”, “let”, or “const” keywords, followed by the variable name and an optional initial value.

31
Q

What is the difference between let and var in JavaScript?

A

The main difference between “let” and “var” in JavaScript is that “let” has block scope, while “var” has function scope.

32
Q

How do you concatenate strings in JavaScript?

A

You can concatenate strings in JavaScript using the “+” operator.

33
Q

What is the difference between == and === in JavaScript?

A

The “==” operator checks for equality in value, while the “===” operator checks for equality in value and data type.

34
Q

How do you create an if statement in JavaScript?

A

You can create an if statement in JavaScript using the “if” keyword, followed by a condition in parentheses and the code to execute if the condition is true in curly braces.

35
Q

What is a for loop and how do you use it in JavaScript?

A

A for loop is a control flow statement that allows you to execute a block of code multiple times. You can use a for loop in JavaScript by using the “for” keyword, followed by a variable declaration, a condition, and an optional increment or decrement.

36
Q

What is a function and how do you define one in JavaScript?

A

A function is a block of code that can be called and executed at any time. You can define a function in JavaScript using the “function” keyword, followed by the function name, parameters in parentheses, and the code to execute in curly braces.

37
Q

How do you access an element in the HTML document using JavaScript?

A

You can access an element in the HTML document using JavaScript by using the “document” object and methods such as “getElementById”, “getElementsByClassName”, or “getElementsByTagName”.

38
Q

What is an array and how do you create one in JavaScript?

A

An array is a data structure that can store multiple values in a single variable. You can create an array in JavaScript by using square brackets, with each value separated by a comma.

39
Q

What is a callback function in JavaScript?

A

A callback function is a function that is passed as an argument to another function and is executed when a specific event or condition occurs.

40
Q

What is the difference between null and undefined in JavaScript?

A

Null is a value that represents the intentional absence of any object value, while undefined is a value assigned to variables that have not been assigned a value.

41
Q

What is the difference between a while loop and a do-while loop in JavaScript?

A

A while loop first checks the condition and then executes the code, while a do-while loop executes the code first and then checks the condition.

42
Q

What is the ternary operator in JavaScript?

A

The ternary operator is a shorthand way of writing an if-else statement that returns a value based on a condition.

43
Q

What is a template literal in JavaScript?

A

A template literal is a string literal that allows you to embed expressions and variables inside the string using backticks (`).

44
Q

How do you add an event listener to an element in JavaScript?

A

You can add an event listener to an element in JavaScript using the “addEventListener” method and specifying the event type and function to execute when the event occurs.

45
Q

What is a constructor function in JavaScript?

A

A constructor function is a function that is used to create new objects with the same properties and methods.

46
Q

What is the spread operator in JavaScript?

A

The spread operator is a syntax that allows you to spread the elements of an array or object as separate arguments or elements.

47
Q

What is hoisting in JavaScript?

A

Hoisting is a mechanism in JavaScript that moves variable and function declarations to the top of their respective scopes.

48
Q

What is the difference between let and const in JavaScript?

A

The main difference between “let” and “const” in JavaScript is that “let” variables can be reassigned, while “const” variables are read-only and cannot be reassigned.

49
Q

What is the difference between synchronous and asynchronous programming in JavaScript?

A

Synchronous programming is when code is executed in a sequential manner, while asynchronous programming allows code to be executed in a non-sequential manner without blocking the execution of other code.

50
Q

What is a promise in JavaScript?

A

A promise is an object that represents the eventual completion (or failure) of an asynchronous operation and allows you to attach callbacks that will be executed when the operation is completed (or failed).