JavaScript Flashcards
javascript vs java
- js is interpreted, java is compiled
- similar syntax
- js is more relaxed
data types
Primitive:
- number (99, 0.8, -1)
- string (“hello”)
- boolean (true or false)
Complex:
- undefined (var x;)
- Objects (special)
- function (user-defined)
objects are special data types that have functions and data associations
javascript arithmetic
- a + b
- a - b
- a / b
- a * b
- a**b (exponential)
- a % b (modulus)
javascript arithmetic shortcuts
- a++ and a– to add or subtract 1 from a number variable
- a+= b is a = a+ b (and same for -,/,*,%)
javascript arithmetic precedence
BEDMAS
brackets, exponents, division/multiplication, addition/subtraction
== and ===
equivalency and strict equivalency; check to see if the value is the same and check to see if the value AND data type are the same
ex. string “5” compared to number 5 is true for == and false for ===
Javascript comparison operators
- == (equal to)
- === (equal to and equal type)
- != (not equal to)
- !== (not equal to or not equal type )
- > (greater than)
- < (less than)
- > = (greater than or equal to)
- <= (less than or equal to)
number to string conversion trick
numberVar = numberVar + “”
concatenation of a number and string type creates a string
control statements
- if/else if/else
- while/do-while loops
- for loops
function syntax
functionName(param1,param2..etc) {
function code
}
deterministic vs nondeterministic loops
for vs while
var vs let vs const
var is function scope let and const is block scope
string concatenations
“hello” + “world” = “helloworld”
“hello” + “ “ + “world” = “hello world”
“hello” + 123 = “hello123”
123 + “” = “123”
Style Guide
- camelCase for identifiers
- avoid $
- identifiers start with _ or letters
JS Events
Things like clicks, entering text, etc.
event handler
function onClick () {
}
html elements associated with events
<button>
<input></input></button>
<textarea>
</textarea>
DOM
Every element (tag) in an HTML page is an object that can be accessed in JS through the document object via dot notation
addEventListener and removeEventListener
takes two parameters, event name and function name
dot notation
Each element’s properties and methods can be accessed using dot notation:
element.style.color = white;
DOM examples
const element = document.querySelector(‘img’);
element.src = ‘newImg.png’;
element.style.background.color = ‘blue’;
changing an element’s class
the classes used in css can be added or removed using:
.classList.add(‘className’);
.classList.remove(‘className’);
event objects
implicitly passed to the handler methods of the function:
function onClick(event) {
const clickSrc = event.currentTarget
}
other DOM properties
element.id
element.value
element.innerHTML (unsecure)
element.textContent
DOM element creation/removal
let element = document.createElement(tag);
element.appendChild(anotherElement);
element.remove(anotherElement);
recursion
-never more efficient as it’s iterative counterpart
-used when the problem itself is recursive by nature (merge/quick sort, tree traversal, balanced strings, etc)
stack frame (activation record)
contains all info necessary for tracing a function call; parameters, local vars, return addresses, etc
when a function is called it’s stack frame is created, initialized, and pushed on to the stack
when a function is finished its stack frame is popped off the stack