###JavaScript's && operator is coalescing.
js> 0 && 7
0
js> 1 && 7
7
&& does not convert things to Boolean values.
/* | |
* Inspired by: http://stackoverflow.com/questions/4360060/video-streaming-with-html-5-via-node-js | |
*/ | |
var http = require('http'), | |
fs = require('fs'), | |
util = require('util'); | |
http.createServer(function (req, res) { | |
var path = 'video.mp4'; |
// factorial | |
function factorial(number) { | |
var product = 1; | |
for (var i = 1; i <= number; i++) { | |
product *= i; | |
} | |
return product; | |
} | |
// factorial recursive |
// with prototype | |
function BankAccount(amount) { | |
this.balance = amount; | |
} | |
BankAccount.prototype.deposit = function(amount) { | |
this.balance += amount; | |
} | |
BankAccount.prototype.withdraw = function(amount) { |
function firstLetter(word) { | |
return word[0]; | |
}; | |
function getAcronym(str){ | |
var words = str.split(" "); // ["for","your","information"] | |
var acr = words.map(firstLetter); // ["f","y","i"] | |
return acr.join("").toUpperCase(); | |
}; |
function isEven(num) { | |
return num % 2 == 0; | |
} | |
function isOdd(num) { | |
return num % 2 != 0; | |
} | |
var nums = []; | |
for (var i = 0; i < 20; ++i) { | |
nums[i] = i+1; | |
} |
// The mutator function for adding array elements to the beginning of an array is unshift(). Here is how the function works: | |
var nums = [2,3,4,5]; | |
console.log(nums); // 2,3,4,5 | |
var newnum = 1; | |
nums.unshift(newnum); | |
console.log(nums); // 1,2,3,4,5 | |
// The second call to unshift() demonstrates that you can add multiple elements to an array with one call to the function. | |
nums = [3,4,5]; | |
nums.unshift(newnum,1,2); |
// JavaScript arrays are only one-dimensional, but you can create multidimensional arrays by creating arrays of arrays. | |
Array.matrix = function(numrows, numcols, initial) { | |
var arr = []; | |
for (var i = 0; i < numrows; ++i) { | |
var columns = []; | |
for (var j = 0; j < numcols; ++j) { | |
columns[j] = initial; | |
} | |
arr[i] = columns; | |
} |
###JavaScript's && operator is coalescing.
js> 0 && 7
0
js> 1 && 7
7
&& does not convert things to Boolean values.
For most simple values, JSON stringification behaves basically the same as toString()
conversions, except that the serialization result is always a string
:
JSON.stringify( 42 ); // "42"
JSON.stringify( "42" ); // ""42"" (a string with a quoted string value in it)
JSON.stringify( null ); // "null"
JSON.stringify( true ); // "true"
The JSON.stringify(..)
utility will automatically omit undefined
, function
, and symbol
values when it comes across them. If such a value is found in an array
, that value is replaced by null
(so that the array position information isn't altered). If found as a property of an object
, that property will simply be excluded.