Reading Notes
JavaScript used in browsers to make websites more interactive
Script a series of instructions a computer can follow to achieve a goal
Vocabulary words that computers understand
Syntax how teh words are put together to create instructions that computers can follow
Computers solve problems programmatically, following a series of instructions, one step after another
Expressions evaluate into a single value
var color = 'beige'; The value of color is now beigevar area = 3 * 2 The value of area is now 6Operators allow programmers to create a single value from one or more values
var color = 'beige'; The value of color is now beigearea = 3 * 2 The value of area is now 6greeting = 'Hi ' + 'Molly';buy = 3 > 5; Value of buy is now falsebuy = (5>3) && (2<4); The value of buy is now trueArithmetic Operators JS contains the standard operators +, -, /, * which can be used with numbers as well as the following special operators
++ (Increment) adds one to the current number-- (Decrement) subtracts one from the current number% (Modulus) divides two values and returns the remainder example: 10 % 3 = 1String Operator just one operator + used to join strings on either side
Mixing numbers and strings
#) create a string, not a numeric number type. Arithmetic operators cannot be used on strings
'2' + '2' = '22' Adding strings concatenates them, it does not add.12 + 'Bob' = '12Bob' Adding a number to a string, creates a new concatenated string'seven' * 'nine' = NaN (Not a Number) attempting to use arithmetic operators on strings will return a value called NaNFunctions allow programmer to group a series of statements together to perform a specific task
{code block} Code blocks contain a series of related steps contained between two curly braces}Declaring a Function give it a name and then write its statements needed to achieve its tasks inside the curly braces
(){}
function sayHello() {
document.write('Hello!');
}
function = Function KeywordsayHello() = Function Name{documnet.write('Hello!');} = Code BlockCalling a Function can execute all of the statements between a functions curly braces with just one line of code in the location that programmer desires
();sayHello();Declaring functions that need information give a function information by providing parameters
() after the function name
function getArea(width,height) {
return width * height;
}
width, height = Parameters used like veriables inside a functionCalling a Function that nees information specify the values it should use in the () following the name, these values are called arguments
getArea(3,5);
wallWidth = 3;
wallHeight = 5;
getArea(wallWidth,wallHeight);
Getting a single value out of a function “returning a result”
function calculateArea(width, height) {
var area = width * height;
return area;
}
var wallOne = calculateArea(3,5);
var wallTwo = calculateArea(8,5);
wallOne variable now holds 15wallTwo variable now holds 40Note: when the interpreter hits the return command it goes back to the statemnt that called it, subsiqent statements in the function would not be processed