Skip to content

Instantly share code, notes, and snippets.

var someVar;
if(someVar) {
// this if statement won't execute, as someVar hasn't been defined
}
// here I just copied a list of nouns off the web.
// 'words' is an array of words because I created a string, then split
// it, like this: "my words".split(" "), which splits that string on the
// spaces.
words = "advice anger answer apple arithmetic badge basket basketball battle beast beetle beggar brain branch bubble bucket cactus cannon cattle celery cellar cloth coach coast crate cream daughter donkey drug earthquake feast fifth finger flock frame furniture geese ghost giraffe governor honey hope hydrant icicle income island jeans judge lace lamp lettuce marble month north ocean patch plane playground poison riddle rifle scale seashore sheet sidewalk skate slave sleet smoke stage station thrill throat throne title toothbrush turkey underwear vacation vegetable visitor voyage year".split(" ");
// syllables will be an array of three letter chunks generated by splitting
// up the words in our previous array. Note that I used the shorthand form
// of creating an array (also called an array literal).
if(typeof someUndeclaredVariable == "undefined") {
// the if statement evaluates correctly-- no errors are thrown!
}
var myVar = true; // some random boolean variable set to true
var undefined = true; // here we "accidentally" defined undefined
if(myVar == undefined) {
// uh oh, this if statement evaluates to true!
}
if(myVar == undefined) {
// var is undefined, oh noes!
}
if(someUndeclaredVar) {
// curses, foiled again! This code throws an error as well.
}
var myString = "Hello, world!";
alert(myString.substr(-6, 5));
>> "world"
var someVar;
if(typeof someVar == "undefined") {
// evaluates to true, as expected
}
var string1 = "Hello ";
var string2 = "world!";
var myStringArray = [string1, string2];
myStringArray.join("");
>> "Hello world!";
["Hello ", "world!"].join("");
>> "Hello world!"