Skip to content

Instantly share code, notes, and snippets.

@dustintheweb
Last active December 21, 2015 03:09
Show Gist options
  • Save dustintheweb/6240306 to your computer and use it in GitHub Desktop.
Save dustintheweb/6240306 to your computer and use it in GitHub Desktop.
Javascript Fundamentals Oft Forgotten
/* ##### General Tips & Syntax ##### */
// scope
var area = 36; // a var with global scope
var volume = function (w, l, h) {
var depth = 15; // a var with local scope
area = w * l; // a var with global scope
};
// line wrap
/* jshint multistr:true */
var text = 'Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric';
// putting a \ at the end of each line makes the string "wrap" to the next line in an editor.
// the jshint line tell jslint to ignore it
// direct check true
var bool = true;
if (bool){
//Do something
}
/* ##### General Functions ##### */
// substring
var fund1='this is a test';
console.log(fund1.substring(6,13)); // output: s a tes
// type check
el = 'yo';
if (typeof(el) !== 'number') {
console.log('not num!');
} else {
console.log('num!');
};
// return
var fund2out = function(){ // return in a function is a way to pass back data from the function.
var fund2in = 1+1;
return fund2in;
}
var fund2transmit = fund2out(); // fund2transmit would now contain the value 2 if outputted.
console.log(fund2transmit);
// prompt
fund3 = prompt('What is your name?');
// confirm
fund4 = confirm('Do you like cheese?')
/* ##### Loops ##### */
// loop increment in mutliples
for (var i = 0; i < 31; i += 3) {
console.log(i); // output in increments of 3
}
// for loops meet conditions via counter
// while loops exist until a condition is met
/* ##### Arrays ##### */
// treat a string like an array of characters
var garble = 'this is a bunch of blah blah blah unicorn blah blah'
console.log(garble[0]); // output: t
// externally add an element to the array
newArray = [];
newArray.push('hello');
console.log(newArray[0]); // output: hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment