JavaScript Overview Flashcards

1
Q

What is a program?

A

A set of instructions for a computer to carry out.

We write programs to solve a specific problem or provide a specific experience. The skill of programming, therefore, has two steps: first, programming is finding solutions to problems; second, programming is implementing those solutions in a particular programming language so your computer can understand and execute your solution.

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

What is JavaScript?

A

JavaScript (JS) is a programming language available in all modern browsers. JS is an implementation of the ECMAScript standard.

We can use the JS programming language, which ECMA determines, to control the engine in a browser. The better you understand the programming language, the better you control the engine.

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

What is ECMA?

A

ECMA controls the standard for JavaScript programming language, which is called ECMAScript. All web browsers should have a JavaScript version that is ECMA compliant. Each web browser is responsible for interpreting the ECMAScript.

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

What is DOM API?

A

Document Object Model API

The part of JavaScript that deals with interacting with HTML elements. It’s specified by the W3C (World Wide Web Consortium), which is the same organization that creates the specification for HTML.

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

What is Chrome DevTools?

A

Chrome DevTools provides an interactive JavaScript console where you can run and debug code.

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

How do you open Chrome DevTools on a MAC?

A

Option + ⌘ + J

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

How do you open Chrome DevTools on a PC?

A

Ctrl + Shift + J

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

What is a variable?

A

A variable is a name that is attached to a value. Variables are used to create and represent data in our applications. (Variables are data in our apps.)

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

How does a variable work?

A

When you need to create data, you create a keyword. Use the var, let, or const keywords to create the data. Then you enter how you want the JS to represent the piece data it’s grabbing:

var someIdentifier

Then you give it an assignment operator:

var someIdentifier = ‘somevalue’;

So here we are telling it to grab this piece of memory (var) with this label (someIdentifier) and set it equal to this value (somevalue).

You can also write it in this way with an undeclared variable:

var someIdentifier;
someIdentifier = somevalue;

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

What is a constant?

A

A constant cannot be reassigned with a new value. It’s used to define constant values, which is another way of saying variables whose value cannot be reassigned.

You can declare it in JS by entering “const”:

const someIdentifier = ‘somevaluethatwon’teverchange’

The defining feature of a constant is that its value cannot be changed, and the browser enforces this behavior. When you try to change the value of a constant, you’ll get an error.

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

In JavaScript, what are the 3 commands we can use to declare space in memory where we’ll store values?

A

var
let
const

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

In JavaScript, what does the equal sign (=) mean?

A

Assignment operator

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

What is let or var?

A

These are used to define variables with values that can (but don’t have to) be changed or reassigned.

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

How can you declare a variable without an assignment operator? What does this do?

A

var someIdentifier;

When you declare a variable without an assignment operator, it has no value yet.

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

What value does JavaScript give declared but unassigned variables?

A

undefined

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

What are two rules for naming variables?

A

Reserved words: You can’t use reserved words in a variable name. For example, you can’t reassign the name var to a new rule like this:

var var = ‘some value’;

However, you can use a reserved word as part of a name though it might be redundant:

var varFoo;

Character restrictions: A variable name must begin with an upper or lower case letter, $, or _. It cannot begin with a number. Spaces cannot appear anywhere in the variable name. Comparison and logical operators cannot be used in variable names. So these are unacceptable:

let my Foo = ‘bar’;
let my+Foo;

Numbers can appear in a variable name, so this is acceptable:

let myFoo2;

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

What are two important stylist guidelines when writing your code in JavaScript?

A

Use camelCasing: A camel-cased variable name starts with a lowercase letter, and then uses lowercase letters throughout, except for the first letter of any new words in the variable name after the first — for these first letters, use uppercase.

Use meaningful variable names: Choose names that reflect how the variable gets used in the program. A well-chosen variable name can help other people reading the code to understand how the variable is intended to be used.

18
Q

How can you force JavaScript to avoid global variables?

A

At the top of your JavaScript, enter this:

‘use strict’;

19
Q

What is a global variable, and why should you avoid it?

A

A global variable is when you enter the variable without the keyword like var or let. Here is an example:

foo = ‘bar’;

The proper way is this:

let foo=’bar’;

Global variables tend to make code buggier and harder to reason about.

20
Q

What is a data type?

A

A data type is a value that variables can have. Data types help us organize our data because there is a lot.

21
Q

How many data types are there and what are they?

A

There are 6 different data types:

String
Number
Boolean
Null
Undefined
Object

Note: Functions are not always considered a data type by everyone.

22
Q

What is coercion?

A

How variables are converted from one type to another in JavaScript. For example:

const stringVar = 'Kilroy was here ';
const numVar = 12;
const combined = stringVar + numVar;
typeof combined; // => string
console.log(combined); // => Kilroy was here 12

The example above demonstrates that if you combine a number with a string using the + operator, the resulting value will be a string.

23
Q

What is typeof in JavaScript?

A

It’s a built-in command JavaScript provides to see the data type of a particular value. So if you run typeof 2, the result would be “number”.

24
Q

What is the string data type?

A

Strings are used to represent text. They include opening and closing quotes (either single or double, but both must be the same kind — you can’t open with a single and close with a double). So. . .

let foo = ‘bar’;

and

let bar = “foo”;

are both valid.

25
Q

What is the number data type?

A

The number data type is used to represent numbers. This includes integer values (whole numbers like 1, 2, 3, 10000000…) and floating point decimal numbers (numbers like 1.234999).

26
Q

What is the null data type?

A

null is a special value used to indicate that a variable has no value.

Note: Be aware that if you run typeof null in JavaScript, you’ll get an eyebrow-raising answer: “object”. Even though null is a distinct data type, typeof says it’s an object. This is a relic of a much earlier version of JavaScript that was not fixed in ES5 or ES6.

27
Q

What is the Boolean data type?

A

Booleans signify truth and falsity. A boolean has two possible values: true and false.

let loggedIn = true;

if (!loggedIn) {
redirectToLoginScreen();
}

In the example above, we set loggedIn to true, and we then have a block of code that runs if and only if the user is not logged in. The ! in !loggedIn inverts the Boolean value of the item to the right of it, so !false evaluates to true and !true evaluates to false.

28
Q

What is the undefined data type?

A

undefined is a special value that the browser assigns to variables when it first creates them in memory. So this:

var foo;

would have a value as undefined in the JavaScript. You should never directly set a value to undefined. It’s fine to check if a variable is undefined, but you shouldn’t do let foo = undefined;. If you want to represent the absence of a value, use null.

29
Q

What is the object data type?

A

Objects allow us to have variables that don’t point to a single value (say, for instance, a single string) but instead to a collection of values. Here’s an example of a JavaScript object:

const person = {
  name: 'Jane Doe',
  greet: function() {
    console.log('Hello world');
  }
};

console. log(person.name); // => Jane Doe
person. greet(); // => Hello world

Here we’ve created an object called person that has a name property and a greet method.

30
Q

What is scope in JavaScript?

A

Scope is about how variables can be accessible (used, reused, and manipulated) within the code. There are two kinds of scope: global and block scope.

31
Q

What is variable scope?

A

Variable scope is a set of rules that define which parts of your code can access a particular variable. It’s what allows us to reuse variable names at different points in our code, without things breaking.

32
Q

What is global scope?

A

When you declare a variable in a global scope, that variable can be accessed anywhere in the code.
When a variable is declared outside of a function, it is in global scope.

33
Q

What is block scope?

A

When you declare a variable in a block scope, the variable is only accessible within the function. When the function is done with its instructions, the variable goes away.

34
Q

What is scope chain?

A

JavaScript understands scope through scope chain. Through scope chain, JavaScript looks at the name of the variable locally to see if it’s defined there.
If it’s not, then it looks at the parent scope of the function to see if it’s defined there. If it’s not, then it continues to search up the chain until it reaches the global variable to find the name.

Thus, if you declare a variable in a function which also has the same name as a global variable, JavaScript will use the local variable vs the global variable.

35
Q

Is it possible for global scope to extend across different files?

A

Yes, it is sometimes possible. If you declare a global variable in one file, you can refer to it in another. In order for this to work, the first file with the global variable needs to be loaded before the second file that is declaring the global variable from the first file.

36
Q

What is a side effect and when can it occur?

A

A side effect occurs when the a function goes outside of the local scope and into the parent scope and alters the code there. It can happen when you use global variables.

37
Q

What is a determinate and indeterminate code?

A

A determinate function is one that will always return the same value if it’s provided with the same inputs. An indeterminate function, in contrast, is one that — given a single set of inputs — returns one value some times and another at other times. This can lead to frustrating and hard-to-track-down bugs in your code. An indeterminate code can occur when you have side effects from using global variables.

38
Q

What is pure code?

A

Has determinate functions and no side effects.

39
Q

How do you prevent global scope?

A

Always use let or const when declaring your variables. Never declare a variable without these keywords.

Enable strict mode in the JavaScript. To do this, you enter ‘use scrict’; at the top of the JavaScript file.

40
Q

When is it okay to use global variables?

A

When you are in a JavaScript library like JQuery.