Basics Flashcards
How do you add a JS script to a HTML page?
<script type="text/javascript"> // Your code goes here ... </script>
How do you include an external JS file in an HTML page?
<script src="path/to/filename.js"></script>
How do you change the contents within a HTML element with id=’target’, with a button click?
<button tyle='button' onclick=' document.getElementById("target").innerHTML = "Hello World!" '>Click Me!</button>
How do you change the font size of an element with id=’target’?
document.getElementById("target").style.fontSize = "32px";
How do you hide an element with id=’target’?
document.getElementById("target").style.display = "none";
Write a basic JS function to change the contents of an element with id=’target’.
<script> function myFunction() { document.getElementById("target").innerHTML = "Hello World!"; } </script>
How do you call an elfunction with a button click?
<button type="button" onclick="myFunction()">Try it</button>
How do you trigger an alert box to pop up?
alert('This is an alert!')
or
window.alert('This is also an alert!')
Create a prompt to get someone’s age
let age = prompt("What's your age?")
What is duck typing?
If it looks like a duck and sounds like a duck it must be a duck!
How can you find out the type of a variable
use the typeof command. For example:
var a = 10; console.log(typeof a)
>>> number
What will ‘typeof null’ return?
>>> 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).
What is the difference between == and ===?
== uses type coercion.
=== compares for equality but without any type coercion.
What is type coercion
Type coercion is where a variable type is change by JS automatically. For example using a number as a boolean.
What is block scope?
It is anything contained within curly braces {}
This is used a lot, if for example if statements.
What is function scope
function scope is anything defined within the function
What is the difference between block and function scope?
Variables created within a block scope are available to the parent scope, this is not the case for function scope.
What is the heap?
A mostly unstructured area of memory where JS objects are stored
What is the stack?
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 do browsers and APIs enhance JS?
Browsers and web API’s are not part of the JS language, but provide extensions to what you can do with the language.