Variables Flashcards

1
Q
var bgColor = "blue";
var textColor = "#FF9933";

var container = document.getElementById(‘container’);

container. style.background = bgColor;
container. style.color = textColor;

Formula:
document.getElementById(“selector”).style.background

A
  • assigning bgColor to the value of “blue” and the textColor to “#FF9933”
  • getting the element by the id of “container”
  • object.property
  • variable built into webpage.method.object.property
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What characters are invalid in a variable name?

A

%, @, -, cannot start with a #

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

undefined vs. null

undefined == null
undefined === null

A
  • undefined works as a keyword/variable
  • null works as an empty value
  • undefined == null —> true
  • undefined === null —> false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What will console log out in the following code?

var part1 = “Team “;

function bam() {
	var part2 = "Treehouse";
	console.log(part2);
}

bam();

A

Treehouse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What will console log out in the following code?

var part1 = “Team “;

function bam() {
	var part2 = "Treehouse";
	console.log(part1);
}

bam();

A

Team

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will console log out in the following code?

var part1 = “Team “;

function bam() {
	var part2 = "Treehouse";
}

bam();

console.log(part1);

A

Team

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will console log out in the following code?

var part1 = “Team “;

function bam() {
	var part2 = "Treehouse";
	function boom() {
		var part3 = "Go ";
		console.log(part3 + part1 + part2);
	}
boom(); }

bam();

A

Go Team Treehouse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will console log out in the following code?

var part1 = “Team “;

function bam() {
	var part2 = "Treehouse";
}

bam();

console.log(part2);

A

ReferenceError: part2 is not defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Scope

A

variables can only be used within its respective curly braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Shadowing

A
  • idea applies to variables with the same name

- if no “var” is declared, then it becomes a global variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will console log out in the following code?

var person = “Jim”;

function whosGotTheFunc() {
	var person = "Andrew";
}

person = “Nick”;

whosGotTheFunc();
console.log(person);

A

Nick

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will console log out in the following code?

var person = “Jim”;

function whosGotTheFunc() {
	person = "Andrew";
}

person = “Nick”;

whosGotTheFunc();

console.log(person);

A

Andrew

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
function elevatorCloseButton(pushed) {
	if (pushed) {
		var status = "I'll close when I'm ready.";
	}
}

elevatorCloseButton(true);

Alter the ‘elevatorCloseButton’ function to follow the best practices in declaring variables within the scope of the function.

A
function elevatorCloseButton(pushed) {
	var status;
	if (pushed) {
		status = "I'll close when I'm ready.";
	}
}

elevatorCloseButton(true);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does it mean to shadow a variable?

A

creating a new variable that has the same name as a variable from a higher scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the correct syntax for creating a variable in JavaScript?

A

var myVariable = “some value”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is it safer to test for undefined using the typeof operator, versus testing against the undefined value?

A

the undefined value can be reassigned, causing unusual bugs

17
Q

In the following code, what value will be displayed in the alert box?

var color = “red”;

function example () {
  color = "blue";
}

example()

alert(color);

A

blue

18
Q

What is the best way to test if a variable myVar is undefined?

A

if (typeof myVar === “undefined”) {}

19
Q

In the following code, what value will be displayed in the alert box?

var color = “red”;

function example () {
  var color = "blue";
}

example()

alert(color);

A

red

20
Q

In JavaScript, a new variable scope is created when?

A

when you open a new function definition

21
Q

What is the value of color after this code is run?

var color;

A

undefined