Post on 18-Jan-2016
JavaScript: The First Parts
Part One
Douglas Crockford
Yahoo! Inc.
Learn to Program
• Values
• Variables
• Expressions
• Branching
• Loops
• Functions
• Recursion
• Arrays
• Objects
• Trees
Why should you learn to program?
In programming we construct and maintain extremely complex systems with
aspirations of perfection.
Ulterior Motives
Programming Languages
JavaScript
The world's most misunderstood programming
language.
Learn to Program
• Values
• Variables
• Expressions
• Branching
• Loops
• Functions
• Recursion
• Arrays
• Objects
• Trees
Values
Numbers, strings, booleans
null
undefined
Variables
Values stored under names.
The values can be replaced.
Expressions
Elements of computation
Branching
Alter the sequential flow of the program
Loops
Repetitive operations
Functions
Encapsulated expressions
Recursion
Functions defined in terms of themselves
Arrays
Linear sequences of storage
Objects
Associative collections of named values
Trees
Complex structures composed of objects
Inside every programming language there is a calculator
• Literal numbers with infix operators
• * used for multiplication
• / is used for division
• Precedence
• ( ) can change precedence
2 + 3 * 4 === 14
(2 + 3) * 4 === 20
The Yellow Box
http://jsmvhs.crockford.com/yellowbox.html
Try these
0.3 - 0.1
0.3 - 0.1 - 0.1 - 0.1
1 / 0
• JavaScript numbers don't always work like real numbers.
The result of an expression can be stored
in a variablea = 3 + 4
a = a + 1
The naming of variables is a serious matter
• It isn't just one of your holiday games.
• Well chosen names can make a program self descriptive.
• A variable name should begin with a letter.
• It can contain any number of letters and digits.
• It can contain the _ (underline) character.
Functions
• Functions can encapsulate an expression.
• Functions can be stored in variables.
• Functions may have parameters.
• Functions can be invoked.
• Functions can grow the language.
function (parameter) { return expression;}double = function (number) { return number * 2;}
double(212)
function (parameter) { return expression;}celsius = function (f) { return (f - 32) * 5 / 9;}
boiling_point = celsius(212)
function (parameter) { return expression;}
quad = function (x) { return double(x) + double(x);}
average = function (a, b) { return (a + b) / 2; }
Functions
• A function can take many parameters, or no parameters.
• If it takes more than one parameter, the parameter names are separated with commas.
• A parameter name is like a variable inside the function.
• When a function is called, the argument values are assigned to the parameters.
Assignment One
1. Read Preface, Chapter One of The Good Parts
2. Write function fahrenheit(c)
3. Write a function of your own that takes two or more parameters.
4. Using trial and error, determine the temperature such that
fahrenheit(x) === celsius(x)
Put your answers in a text or html file and add it to http://groups.yahoo.com/group/jsmvhs/files/Assignment1/
Use your unique ID in the name of the file.