JavaScript
Variables
Variables
get type of variable
var $data = "blah";console.log($.type($data));print variable in a string (backtick)
console.log(`this is my ${variable}`);variable types
let myLet = "some var that can change later";const myConst = "constant value that wont change";Arrays
Arrays
add to array
const myArray = ["apple", "banana"];myArray.push("watermelon");remove from array
const index = myArray.indexOf("banana");if (index > -1) { // only remove item if element is found myArray.splice(index, 1); // 2nd param means to remove 1 item only}loop over array elements
myArray.forEach(element) => { console.log(element);}get size of array
var size = arr.length;Conditionals
Conditionals
Hashes
Hashes
If /Else
let cheese = 'Cheddar';
if (cheese == 'Cheddar') {
console.log('this is cheddar');
} else if (cheese == 'Swiss') {
console.log('This is Swiss');
} else {
console.log('no cheese');
}
Logical Operators
&& = AND
|| = OR // if (name == 'frank' || name == 'bob')
! = NOT // if ( ! (name == 'frank' || name == 'bob'))
JQUERY
JQUERY
Parse through Select field option values
$.map($('#sel_customer option'), function(option){
if (option.value === "myOption") {
console.log("myOption is in options")
}
});
get array of <div> IDs from any <div> with class of "myclass"
<button onclick="getIDs('myclass')"> click me </button>
<script>function getIDs(selector) { const elements = document.querySelectorAll('div.'+selector);
const ids = Array.from(elements).map(element => element.id); return ids.join(','); // convert array to string}
</script>