JavaScript Flashcards
What is a varibale in JavaScript?
A variable is a named container (space in the memory) that stores some value.
The main purpose of variables is to store some information in them. Variables are labeled with names so that we may easily access the information contained within them when we need it. A common way of saying “access a variable” is “reference a variable by its name”.
You decided to pack all your stuff into different boxes and place them in your garage. You put a label on each box so that it’s easier to find it later and obviously, the name is descriptive, so you know what is in that box.
- the garage is the memory,
- the box is the variable,
- the box label is the variable’s name,
- the box content is the variable’s value.
What is meant by the term ‘variable decleration’?
A variable declaration is when you create a variable and give it a name.
When we declare a variable, we create storage space in the memory and give it a name (label) by which we can access it later.
To declare a variable, we can use the keyword let or const and in older versions of JavaScript, you would find the keyword var. To start, we’ll use let to declare variables. Later in the lesson, we’ll explain how to use const.
What is meant by the term ‘variable initialization’?
Variable initialization is when you assign some value to a newly declared variable.
After declaring a variable, you can assign it a value. This process is called variable initialization.
Example
let company; <- decleration
company = ironhack; <- initialization
What is the default value of a variable?
We you declare a variable but you don’t initialize it, the default value is ‘undefined’
What’s the difference between a variable decleration with ‘let’ and ‘const’?
let is used to declare variables whose value may change in the future.
const is used to declare a variable that will remain constant and whose value can’t be reassigned.
Note: a constant variable must be declared and initialized at the same time
What naming rules are there for variable naming in JavaScript?
Variable Naming Rules
1) Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
2) Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
3) Variable names must not contain spaces.
4) Variable names are case sensitive (Name, name and NAME are all different variables)
5) Reserved keywords cannot be used as variable names.
What is meant by the naming convention camelCase?
camelCase (see also) https://en.wikipedia.org/wiki/Camel_case
Used for variable naming in Javascript, because it should inc rease readability, when there are more than one word:
Begin words or abbreviations in the middle of the phrase with a capital letter. The purpose of this practice is to enhance readability.
Example:
let myAge = 18;
let yourEyeColor = “green”;
However, there are more conventions like:
- camelCase
- PascalCase
- snake_case
- kebab-case
Which data types do exist in JavaScript?
There are primitive data types and non-primitive data types:
1) Primitive data types are
1) string »_space; let name = “Hello”;
2) number »_space; let price = 1034;
3) boolean »_space; let isActive = true;
4) undefined »_space; let myUndefined = undefined;
5) null »_space; let myNull = null;
(6) bigInt_ and symbol_)
2) Non-Primitive data types are
1) object »_space; let student = { name: “Ana”, age: 40 };
2) array »_space; let numbers = [7, 23, 31, 45, 60];
What is meant by the typeof operator?
The typeof operator gives me the type of the variable.
Example:
let favoriteFood;
favoriteFood = ‘Steak’;
console.log(typeof favoriteFood); // <== string
Read more here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
What is meant by the term expression?
An expression is a combination of any values (number, string, array, object) and operators that produces another value.
Data Type String:
What is a string?
A string is a sequence of characters (text).
A string can contain any character: letter, number, punctuation, or even new lines and tabs.
To create a string, we wrap a piece of text with quotes. You can use any of the following types of quotes when creating a string:
”” - double quotes,
‘’ - single quotes and
`` - backticks (in most keyboards, it is placed above the tab in the upper-left corner).
Backticks `` allow string interpolation, which we’ll explain in the next step
Data Type String:
What is meant by string interpolation?
String interpolation is the process of embedding variable values into a string using placeholders.
In JavaScript, string interpolation is available for strings created with backticks `` .
Example:
const name = “John”;
const surname = “Doe”;
const greeting = Hello, my name is ${name} ${surname}!!
;
console.log(greeting);
Data Type String:
What is a multiline string?
A multiline string is a long string that spans multiple lines.
In JavaScript, multiline string is available for strings created with backticks `` only.
Data type String:
What is meant by Concatenation?
Concatenation is the process of appending (adding) one string to the end of another string.
We can do that by + or += (this adds the new string to the previous one)
Example:
let greeting = “”;
greeting += “Hello there.”;
console.log(greeting); // ==> Hello there.
greeting += “ Welcome to Ironhack!”
console.log(greeting); // ==> Hello there. Welcome to Ironhack!
However, we can also add strings and numbers by +
Example:
let birthYear = 1990;
let birthDate = “My birthday is on May 8th “;
let evasBirthday = birthDate + birthYear;
console.log(evasBirthday);
console.log(typeof evasBirthday) // <== “string”»_space; string + number is turned into string
Data Type String:
How can you access the length of a string?
By adding . length at the end.
Example:
let evasHobby = “Coding is my favorite hobby”;
console.log(evasHobby.length) // <== 27
Data Type String:
How can you index a string?
A string character can be indexed by adding a bracket at the end with the number of the indexed character. However, you must be aware that JavaScript always starts counting at 0.
Example:
let evasHobby = “Coding is my favorite hobby”;
console.log(evasHobby[0]) // <== C
console.log(evasHobby[11]) // <==y
Data Type String:
How would you check if a string has a certain character? And how does it work?
You can check with the method
.indexOf(“String you search for”);
E.g.
let evasHobby = “Coding is my favorite hobby”;
console.log(evasHobby.indexOf(“Coding”)) // <== returns 0 as it starts there
console.log(evasHobby.indexOf(“o”)) // <== returns 1, as it will always name ths index of the first found letter
console.log(evasHobby.indexOf(“z”)) // <== returns -1 as non existent
Data Type String:
How would you cut out a part of a string without modifying the original string?
You can do it with the method
.slice(index start, index end)
The string method slice() extracts a part of a string and returns it as a new string without modifying the original string.
let evasHobby = “Coding is my favorite hobby”;
let cutOut = evasHobby.slice(10,21)
console.log(cutOut) // <== “my favorite”
Data Type String:
How would you turn all letters of a string to uppercase?
How can you achieve just turning the first letter to uppercase?
You can do it with the method
.toUpperCase()
All letters:
let evasHobby = “Coding is my favorite hobby”;
evasHobby = evasHobby.toUpperCase();
First letter only:
let name = “sandra”;
let firstCharacterUp = name[0].toUpperCase()
let formattedName = firstCharacterUp + name.slice(1);
console.log(formattedName) // <== “Sandra”
vereinfacht auch
let name = “sandra”;
let formattedName = name[0].toUpperCase() + name.slice(1)
console.log(formattedName);
Data Type Number:
What is the difference between an integer and floating number?
Integer: a whole number, such as 1, 12, 256, 10000, -5, -83, etc.
Floating-point number: a number with a decimal point, such as 0.2, 5.5, -123.55, etc
Data Type Number:
Name all the operators you know in JavaScript
Basic:
- Addition: a + b
- Substraction: a - b
- Multiplication: a*b
- Division: a / b
Advanced:
- Exponential: a ** 2 // <== a * a * a ==> a^3
- Modulo: a % b // <== gives the remainder of a/b; if a is a multiple of a it will be 0
Note: to check if you have a odd number all you need to do is check if the modulo %2 === 0»_space; must retur ‘true’
for odd number modulo %2 ===1»_space; must be true
Data Type Number:
What is meant by an Advanced Assignment Operator?
Assignment Operator:
Advanced Assignment Operator:
x += y »_space; Addition Assignment»_space; x = x+y
x -= y»_space; Substraction Assignment»_space; x = x-y
x = y»_space; Multiplication Assignment»_space; x = xy
x /= y»_space; Division Assignment»_space; x = x/y
x %= y»_space; Remainder Assignment»_space; x = x%y
x **= y»_space; Exponentiation Assignment x = x ** y
Data Type Boolean:
What is meant by the Boolean Data Type?
Boolean data type can have one of two possible values: true or false.
Booleans are used to represent two states, such as if a light is ON or OFF, or to indicate if something is true or false.
Data Type Boolean:
What are the comparision operators?
Comparison operators are used to check if two values or variables are equal or different.
Expression Description
exp1 == exp2 Returns true if the expressions have the same
value
exp1 === exp2 Returns true if the expressions have the same
value and the same data type of value.
exp1 != exp2 Returns true if the expressions do not have the
same value.
exp1 !== exp2 Returns true if the expressions do not have the
same value or the same type of value.
exp1 < exp2 Returns true if the value of exp1 is smaller than the
exp2 value.
exp1 > exp2 Returns true if the value of exp1 is bigger than the
exp2 value.
exp1 <= exp2 Returns true if the value of exp1 is smaller or equal
than the exp2 value.
exp1 >= exp2 Returns true if the value of exp1 is bigger or equal
than the exp2 value.
Data Type Boolean:
What is meant by a Boolean expression?
A boolean expression is simply an expression that turns true or false
Data Type Boolean:
What is meant by strict equality?
Strict equality means that the operator checks for same value AND for same data type.
The operator is the 3 equal signs: ===
It is recommended to always use this operator to prevent bugs.