Javascript Basics: Mendel Rosenblum
Javascript Basics: Mendel Rosenblum
Mendel Rosenblum
... has an API for working with text, arrays, dates and regular expressions
1/0 == Infinity
Math.sqrt(-1) == NaN Nerd joke: typeof NaN returns 'number'
Watch out:
(0.1 + 0.2) == 0.3 is false // 0.30000000000000004
bitwise operators (e.g. ~, &, |, ^, >>, <<, >>>) are 32bit!
CS142 Lecture Notes - JavaScript Basics 9
string type
string type is variable length (no char type)
let foo = 'This is a test'; // can use "This is a test"
foo.length // 14
● Falsy:
● Truthy:
● Both are falsy but not equal (null == undefined; null !== undefined)
CS142 Lecture Notes - JavaScript Basics 12
function type
var foobar = function foobar(x) { // Same as function foobar(x)
if (x <= 1) {
return 1;
}
return x*foobar(x-1);
}
typeof foobar == ‘function’; foobar.name == 'foobar'
Many methods for returning and setting the data object. For example:
date.valueOf() = 1452359316314
date.toISOString() = '2016-01-09T17:08:36.314Z'
date.toLocaleString() = '1/9/2016, 9:08:36 AM'
Uses:
Searching: Does this string have a pattern I’m interested in?
Parsing: Interpret this string as a program and return its components
'12e34'.search(/[^\d]/); // Returns 2
'foo: bar;'.search(/...\s*:\s*...\s*;/); // Returns 0
<script type="text/javascript">
//<![CDATA[
Javascript goes here...
//]]>
</script>