Javascript Flashcards
What are the different types of values in Javascript
numbers, strings, booleans, objects, functions, and undefined values
What is the modulo and what does it do?
It is the % symbol and it represents the remainder operation
X % Y
What happens in this case 3 * 11 +4
It calculates it using precedence
What are the three special values in Javascript?
Infinity, -Infinity, and NaN (not a number)
What value is “throw that away!”
A string
In Javascript, how does a program keep an internal state? How does it remember things?
To catch and hold values, JavaScript provides a thing called a variable.
var caught = 5 * 5;
What’s an expression?
A fragment of code that produces a value is called an expression. Every value that is written literally (such as 22 or “psychoanalysis”) is an expression.
What’s the difference between an expression and statement?
If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a full sentence in a human language. A program is simply a list of statements.
Give an example of an expression.
1;
!false;
What is a statement able to do that an expression can’t?
A statement stands on its own and amounts to something only if it affects the world. It could display something on the screen—that counts as changing the world—or it could change the internal state of the machine in a way that will affect the statements that come after it. These changes are called side effects.
What’s happening?
var mood = "light"; console.log(mood); // → light mood = "dark"; console.log(mood); // → dark
When a variable points at a value, that does not mean it is tied to that value forever. The = operator can be used at any time on existing variables to disconnect them from their current value and have them point to a new one.
Variables are akin to?
You should imagine variables as tentacles, rather than boxes. They do not contain values; they grasp them—two variables can refer to the same value. A program can access only the values that it still has a hold on. When you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.
Can a single var statement define multiple statements?
Yes, A single var statement may define multiple variables. The definitions must be separated by commas
var one = 1, two = 2; console.log(one + two); // → 3
What’s a keyword?
Words with a special meaning, such as var, are keywords, and they may not be used as variable names. There are also a number of words that are “reserved for use” in future versions of JavaScript. These are also officially not allowed to be used as variable names, though some JavaScript environments do allow them.
What’s an environment?
The collection of variables and their values that exist at a given time is called the environment. When a program starts up, this environment is not empty. It always contains variables that are part of the language standard, and most of the time, it has variables that provide ways to interact with the surrounding system. For example, in a browser, there are variables and functions to inspect and influence the currently loaded website and to read mouse and keyboard input.
What’s a function?
A lot of the values provided in the default environment have the type function. A function is a piece of program wrapped in a value. Such values can be applied in order to run the wrapped program. For example, in a browser environment, the variable alert holds a function that shows a little dialog box with a message. It is used like this:
alert(“Good morning!”);
How do you execute a function and what’s it called?
Executing a function is called invoking, calling, or applying it. You can call a function by putting parentheses after an expression that produces a function value.
What are the values that go in the parentheses?
The values between the parentheses are given to the program inside the function. In the example, the alert function uses the string that we give it as the text to show in the dialog box. Values given to functions are called arguments. The alert function needs only one of them, but other functions might need a different number or different types of arguments.
Give an example of an argument
alert(“Good morning!”);
What’s the purpose of the console.log function?
The alert function can be useful as an output device when experimenting, but clicking away all those little windows will get on your nerves. In past examples, we’ve used console.log to output values.
What’s a side effect?
Showing a dialog box or writing text to the screen is a side effect. A lot of functions are useful because of the side effects they produce. Functions may also produce values, and in that case, they don’t need to have a side effect to be useful.
What’s a return value?
When a function produces a value, it is said to return that value. Anything that produces a value is an expression in JavaScript, which means function calls can be used within larger expressions.
What’s happening here:
console.log(Math.min(2, 4) + 100); // → 102
Here a call to Math.min, which is the opposite of Math.max, is used as an input to the plus operator:
What happens when user selects OK when a confirm pops up and what happens in the following code?
confirm(“Shall we, then?”);
This returns a Boolean: true if the user clicks OK and false if the user clicks Cancel.
What happens here?
prompt(“Tell me everything you know.”, “…”);
The prompt function can be used to ask an “open” question. The first argument is the question, the second one is the text that the user starts with. A line of text can be typed into the dialog window, and the function will return this text as a string.