LIS2360 lesson 6
Write the code to display "You are X", where X is the value of the age variable. age = 21; console.log(_________);
"You are " + age
non-identity operator
!== is the opposite of the identity operator
anonymous function
a function that does not have a name
method
a function that is attached to an object and operates on data stored in the object
console
a location where text output is displayed
infinite loop
a loop that never stops executing
while loop
a looping structure that checks if the loop's condition is true before executing the loop body, repeating until the condition is false
A JavaScript coding convention is to name JavaScript variables with camel casing, where the identifier starts with
a lowercase letter, and subsequent words begin with a capital letter. Ex: lastPrice
indentifier
a name created by a programmer for an item like a variable or function
function
a named group of statements
truthy
a non-Boolean value that evaluates to true in a Boolean context
let
a variable can be declare with let
local scope
a variable declared inside a function the function that defines the variable has access to the local variable
global scope
a variable declared outside a function all functions have access to a global variable
What is output to the console? ar autos = ["Chevrolet", "Dodge", "Ford", "Ram"]; for (i = 0; i < autos.length; i++) { if (i % 2 == 0) { console.log(autos[i]); } }
chevrolet, ford (only elemnets with even indexes are printed)
What is output to the console? var autos = ["Chevrolet", "Dodge", "Ford", "Ram"]; autos.forEach(function(item, index) { if (index % 3 == 0) { console.log(item); } });
chevrolet, ram (only elements with indexes that are evenly divisible by 3 are output)
compound assignment operator
combines an assignment statement with an arithmetic operation
switch statement
compares an expression's value to several cases using strict equality (===) and executes the first matching case's statements
length
contains the number of elements in the array
parseInt() and parseFloat()
convert strings into numbers
NaN
convert strings into numbers isNaN() returns true if the argument is not a number, false otherwise
Which identifier is illegally named? a. star_destroyer b. ADDRESS c. $save d. 9to5
d. 9to5
object
data type collection of property and value pairs
string
data type group of characters delimited with 'single' or "double" quotes
null
data type intentionally absent of any object value
array
data type list of items
numbers
data type numbers with or without decimal places
boolean
data type true or false
undefined
data type variable without a value
array object
defines numerous methods for manipulating arrays
dynamic typing
determines a variable's type at run-time
console.log() function
displays text or numbers in the console
Which loop always executes the loop body at least once?
do-while loop
JavaScript engines
modern JavaScript interpreters
x = 10; var y = 20;
no error
Write the function call to display the question variable to the user and retrieve the user's age. question = "How old are you?"; age = __________;
prompt(question)
prompt() function
prompts the user with a dialog box that allows the user to type a single line of text and press OK or Cancel and returns the string the user typed or null if the user pressed Cancel
shift()
removes the first array element and returns the element
pop()
removes the last array element and returns the element
indexOf() and lastIndexOf()
search an array and return the index of the first found value or -1 if the value is not found indexOf() searches beginning to end, lastINdexOf() searches end to beginning
for-of
statement is a simplified for loop that loops through an entire array
break statement
stops executing a case's statements and causes the statement immediately following the switch statement to execute
scope
the context in which the variable can be accessed
ECMAScript
the standard that has been improved over the years, and JavaScript is an implementation of ECMAScript. The latest version of ECMAScript is version 9 (ES9) and was released in 2018
loop body
the statements that a loop repeatedly executes
block scope
the variable is accessible only within the enclosing pair of curly braces
artists.indexOf("Botticelli") returns 3. var artists = ["Raphael", "Titian", "Masaccio", "Botticelli", "Titian"];
true
if (" ")
true
if (999)
true
if (myArray)
true
Array elements that are not assigned a value are
undefined
just-in-time (JIT) compilation
used by JavaScript engines to compile the JavaScript code at execution time into another format that can be executed quickly.
elements
values in an array
What is missing in the for-of loop to display all the elements in the autos array? var autos = ["Chevrolet", "Dodge", "Ford", "Ram"]; for (____) { console.log(auto); }
var auto of autos (each array element in autos is assigned to the auto variable, which is logged to the console)
What is autos.length? var autos = ["Chevrolet", "Dodge", "Ford", "Ram"];
4
What numbers are output by the code segment? for (c = 5; c < 10; c +=2;) { console.log(c);
5,7,9
identity operator
=== performs strict equality two operands are strictly equal if the operands' data types and values are equal
JavaScript background
Brendan Eich created JavaScript in 1995 so the Netscape Navigator browser could dynamically respond to user events JavaScript was standardized by Ecma International in 1997 and called ECMAScript
hoisting
JavaScript's behavior of moving variable declarations to the top of the current scope
What is syntactically wrong with the following code? name = 'Danny O'Sullivan' a. name is assigned a value without being declared first b. variables may not be assigned strings delimited with single quotes c. the single quotation mark in O'Sullivan
c. the single quotation mark in O'Sullivan
Which variable is named with the preferred JavaScript naming conventions? a. total_points b. $totalPoints c. totalPoints
c. totalPoints
continue
causes a loop to iterate again without executing the remaining statements in the loop
What is output to the console? var autos = ["Chevrolet", "Dodge", "Ford", "Ram"]; for (i = 0; i < 2; i++) { console.log(autos[i]); }
chevrolet, dodge
What is the data type of z? var = z; a. undefined b. string c. number
a. undefined
unshift(value)
adds a value to the beginning of the array
push(value)
adds a value to the end of the array
splice(startingIndex, totalElementsToDelete, valuesToAdd)
adds or removes elements from anywhere in the array and returns the deleted elements (if any)
forEach()
also loops through an array method takes a function as an argument
arrow function
an anonymous function that uses an arrow => to create a compact function
constant
an initialized variable whose value cannot change usually named with an identifier in all capital letters keyword: const
global object
an object that stores certain global variables, functions, and other properties
array
an ordered collection of values
comment
any text intended for humans that is ignored by the JavaScript interpreter
string concatenation
appends one string after the end of another string, forming a single string
What is the data type of x? y = false; x = y; a. string b. boolean c. number
b. boolean
Which statement declares a constant for Earth's gravity? a. var EARTH_GRAVITY = 9.8; b. const EARTH_GRAVITY = 9.8; c. const EARTH_GRAVITY;
b. const EARTH_GRAVITY = 9.8;
Which statement declares the variable sum without assigning a value to sum? a. sum; b. var sum; c. sum=0;
b. var sum;
break
breaks out of a loop prematurely
Which condition causes the for loop to output the numbers 100 down to 50, inclusively? for (c = 100; ____________; c--;) { console.log(c);
c >=50
index
each array element is stored in a numeric location
/* a is assigned 2 a = 2;
error
3.12 = pi;
error
if-else statement
executes a block of statements if the statement's condition is true, and optionally executes another block of statements if the condition is false
interpreter
executes programming statements without first compiling the statements into machine language
for-loop
executes the initialization expression, evaluates the loop's condition, and executes the loop body if the condition is true
do-while loop
executes the loop body before checking the loop's condition to determine if the loop should execute again, repeating until the condition is false
All browsers must use the same JavaScript engine
false
ECMAScript and JavaScript are the same thing
false
JavaScript and Java are the same programming language
false
JavaScript is only used for programs that run in a web browser
false
if ("")
false
if (0)
false
if (NaN)
false
if (undefined)
false
Which loop is best for looping through an array in reverse order (from last element to first element)?
for loop (the for-of and forEach() loops always loop through an array starting with the first element and ending at the last element.) the for loop provides more control than the other loops over the order in which array elements are accessed
conditional operator
has three operands
function expression
identical to a function declaration, except the function name is omitted
variable
is a named container that stores a value does not have to be declared before being assigned a value keyword: var
falsy
is a non-Boolean value that evaluates to false in a Boolean context
self-invoking function
is an anonymous function that invokes (calls) itself