Javascript Flashcards
What is a console?
The console is a panel that displays important messages, like errors, for developers. If we want to see things appear on our screen, we can print, or log, to our console directly.
In JavaScript, the console keyword refers to an object, a collection of data and actions, that we can use in our code.
console.log()
When we write console.log() what we put inside the parentheses will get printed, or logged, to the console. This is important so we can see the work that we’re doing.
The console.log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
What are the two types of code comments in Javascript?
A single line comment and a multi-line comment
How do you write a single line comment?
// + comment
Note: You can also write a single line comment after a line of code.
How do you write a multi-line comment?
/* to begin the comment, and / to end the comment.
Note: you can also use this syntax to comment something out in the middle of a line of code.
Ex: console.log(/IGNORED!*/ 5); // Still just prints 5
When is it a good time to use multi-line comments?
When preventing a block of code from running.
What are data types? How many fundamental data types are there?
Data types are the classifications we give to the different kinds of data that we use in programming. There are 7 fundamental data types.
What are the seven different fundamental data types?
Number, string, boolean, null, undefined, symbol, and object.
What is a string data type?
Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes: ‘ … ‘ or double quotes “ … “
What is a boolean data type?
A data type that only has two possible values- true or false. You can think of it as answers to a “yes” or “no” question.
What is a null data type?
This data type represents the intentional absence of a value, and is represented by the keyword null
What is an undefined data type?
Uses the keyword undefined (without quotes) and means that a given value does not exist. It represents the absence of a value like null but is used differently.
What is an object data type?
Collections of related data.
What are literals in JS?
Literals represent values in JavaScript. These are fixed values—not variables—that you literally provide in your script.
What are identifiers in JS?
Identifiers are JavaScript names. They are used to name variables and keywords, and functions.
What are the rules for identifiers in JS?
The rules for legal names are the same in most programming languages. A JavaScript name must begin with:
A letter (A-Z or a-z)
A dollar sign ($)
Or an underscore (_)
Subsequent characters may be letters, digits, underscores, or dollar signs.
What are expressions?
An expression is a single unit of JavaScript code that the JavaScript engine can evaluate, and return a value.
An expression can be a literal, like a string or a number. It can also be an identifier that points to a variable.
Name three types of simple expressions in JS.
Arithmetic expressions,
String expressions,
Logical expressions.
Explain arithmetic expressions
Arithmetic expressions are expressions that take a variable and an operator (more on operators soon), and result in a number:
1 / 2
i++
i -= 2
i * 2
Explain logical expressions
Logical expressions make use of logical operators and resolve to a boolean value:
a && b
a || b
!a
Explain string expressions
String expressions are expressions that result in a string:
‘A ‘ + ‘string’
What is an operator?
An operator is a character that performs a task in our code.
What is the keyboard symbol for the multiply operator?
- Ex: 7 * 3 = 21
What is the keyboard symbol for the division operator?
/
Ex: 9/3 = 3
What is the keyboard symbol for the remainder operator?
%
console.log(11 % 3); // Prints 2
What is the remainder operator sometimes called?
modulo
Name three of the operator precedence rules in JS
The precedence rules are:
- / % (multiplication/division/remainder)
+ - (addition/subtraction)
= (assignment)
Operations on the same level (like + and -) are executed in the order they are found, from left to right.
What is string concatenation?
Combining one string to another string use the + operator.
Ex: console.log(‘hi’ + ‘ya’); // Prints ‘hiya’
How would we include space in between two strings when we are using string concatenation?
We have to include empty parenthesis in between them or space at the end or beginning of a string.
Ex:
console.log(‘front ‘ + ‘space’);
// Prints ‘front space’
console.log(‘back’ + ‘ space’);
// Prints ‘back space’
console.log(‘no’ + ‘space’);
// Prints ‘nospace’
console.log(‘middle’ + ‘ ‘ + ‘space’);
// Prints ‘middle space’
What are properties?
Properties are the values associated with a JavaScript object.
What is the symbol used for the dot operator?
.
Ex: console.log(‘Hello’.length); // Prints 5
The JavaScript spread operator
The JavaScript spread operator (…) expands an iterable (like an array) into more elements. This allows us:
- to quickly copy all or parts of an existing array into another array* (when we want to create a new array from an existing array),
const fruits = [‘banana’, ‘pear’, ‘apple’]
const morefruits = […fruits]
console.log(morefruits)
//[ ‘banana’, ‘pear’, ‘apple’ ]
*only works with primitive data types
- to extract only what’s needed from an array,
const numbers = [1, 2, 3, 4, 5, 6];
const [one, two, …rest] = numbers;
What are methods?
Methods are actions we can perform. Data types have specific actions we can perform that allow us to handle instances of that data type.
How do we use or “call” methods?
We call, or use, methods by attaching an instance with:
a period (the dot operator)
the name of the method
opening and closing parentheses
Ex:’example string’.methodName().
Ex: console.log(‘hello’.toUpperCase()); // Prints ‘HELLO’
.random()
This method returns a random number between 0 (inclusive) and 1 (exclusive). Math.random() always returns a number lower than 1.To generate a random number between 0 and 50, we could multiply this result by 51, like so:
Math.random() * 51;
Math.floor()
This method takes a decimal number, and rounds down to the nearest whole number.
Ex:
Math.floor(Math.random() * 50);
Math.ceil()
The Math.ceil() function always rounds up to the nearest integer.
Ex:
console.log(Math.ceil(.95));
// expected output: 1
Ex:
console.log(Math.ceil(4));
// expected output: 4
console.log(Math.ceil(7.004));
// expected output: 8
Number.isInteger()
method determines whether the passed value is an integer.
Ex:
console.log(Number.isInteger(2017))
//Prints true
How can we access properties and methods?
We can access properties and methods by using the ., dot operator.
What is a variable?
A variable is a container for a value. They are like little containers for information that live in a computer’s memory. You can have container, label it, and put “stuff” in it.
What three things can we do with variables?
- Create a variable with a descriptive name.
- Store or update information stored in a variable.
- Reference or “get” information stored in a variable.
var
var, short for variable, is a JavaScript keyword that creates, or declares, a new variable.
Ex:
var myName = ‘Arya’;
console.log(myName);
// Output: Arya
What is myName when creating a variable?
This would be the variable name (labeling the container). It is standard convention to use camel casing.
Ex:
var myName = ‘Arya’;
console.log(myName);
// Output: Arya
What is camel casing?
In camel casing you group words into one, the first word is lowercase, then every word that follows will have its first letter uppercased.
Ex:
camelCaseEverything