____Holds reusable data in a program and associated it with a name
What are Variables?
Variables that have not been initialized store the primitive data type _____.
undefined
Beware:
There are a few general rules for naming variables:
The following will print . . .
let tool = 'marker';
// Use short circuit evaluation to assign writingUtensil variable below:
let writingUtensil = tool || 'pen';
console.log(`The ${writingUtensil} is mightier than the sword.`);
The marker is mightier than the sword.
This will print:
console.log(Math.floor(43.8));
43
The ____ object is used to represent and manipulate a sequence of characters.
The String object is used to represent and manipulate a sequence of characters.
Using template literals to embed variables into strings is called ____.
What is string interpolation?
This data type represents the intentional absence of a value, and is represented by the keyword ____(without quotes).
Null
The following code will print
let username = '';
let defaultName = username || 'Stranger';
console.log(defaultName);
// Prints: Stranger
console.log(Math.ceil(43.8));
44
This will print:
console.log(' Remove whitespace '.trim());
Remove whitespace
.trim() - Use a string method to log the following string without whitespace at the beginning and end of it.
What is the outcome of the following code snippet?
console.log('Hello World'.length);
11
This data type is denoted by the keyword ____(without quotes). It also represents the absence of a value though it has a different use than null.
Undefined
This will print:
let username = '';
let defaultName;
if (username) {
defaultName = username;
} else {
defaultName = 'Stranger';
}
console.log(defaultName);
// Prints: Stranger
So which values are falsy— or evaluate to false when checked as a condition? The list of falsy values includes:
0
Empty strings like "" or ''
null which represent when there is no value at all
undefined which represent when a declared variable lacks a value
NaN, or Not a Number
console.log(Number.isInteger(2022));
true
This prints:
// Modulo
console.log(10 % 5);
0
True or False - The correct way to call the random method on the Math global object is _____?
math.random;
False
Math.random()
A newer feature to the JS language, symbols are unique identifiers, useful in more complex coding.
Symbol
let athleteFinalPosition = 'first place';
switch (athleteFinalPosition){
case 'first place':
console.log('You get the gold medal!');
break;
case 'second place':
console.log('You get the silver medal!');
break;
case 'third place':
console.log('You get the bronze medal!');
break;
default:
console.log('No medal awarded.');
break;
}//what is printed when running the switch statement?
You get the gold medal!
This will print:
'hello'.toUpperCase()
HELLO
a ____ variable cannot be reassigned because it is _____. If you try to reassign this variable, you’ll get a TypeError.
const constant
What would the following code snippet print?
const food = 'chicken';
food = 'sushi';
An error
A Collections of related data is called
Object
which of the following variables contains a trutht value?
a. let varOne = 'false'; b. let varTwo = false;
c. let varThree = 0; d. let varFour = ' ';
a. let varOne = 'false';
non-empty strings are truthy!
This will print:
console.log('Hey'.startsWith('H'));
Prints: true
//This will print
const entree = "Enchiladas";
console.log(entree);
entree = 'Tacos';
Enchiladas
/home/ccuser/workspace/learn-javascript-variables-constV2/main.js:3 entree = 'Tacos'; ^
TypeError: Assignment to constant variable. at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-variables-constV2/main.js:3:8) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:427:7) at startup (bootstrap_node.js:151:9) at bootstrap_node.js:542:3