Javascrip Code Camp Flashcards
what does “immutable” mean when it comes to programming?
immutable - once they are created, they cannot be changed.
However, the variable assigned to the immutable value can still be reassigned another value.
Strings are immutable.
declaring a variable vs. initializing a variable
declaring a variable:
let greeting;
initializing a variable:
greeting = “Hello World”;
primitive vs. non-primitive data types
Non-primitive data types differ from primitive data types in that they can hold more complex data. Primitive data types like strings and numbers can only hold one value at a time.
An array is a non-primitive data type.
How do you access something in an array?
nameOfArray[n]
where n is the index number of the item in the array.
Remember arrays always start with 0.
what function counts the number of items in an array?
.length
arrayName.length
By subtracting 1 from this ^^^, you get the index of the last element in the array.
Method vs. Function
1 distinction
A method in JavaScript is a function that’s associated with certain values or objects.
console.log
the .log() method, is part of the “console” object.
.push()
.push() method
“pushes” a value to the end of an array
It RETURNS the new length of the array, after adding the value you give it.
.pop()
.pop() method
It removes the last element from an array and RETURNS that element.
It really removes it. “Pops” it off the array.
const vs. let
2 distinctions
- a const variable cannot be reassigned like a let variable. This code would throw an error:
const firstName = “Naomi”;
firstName = “Jessica”; - A const variable cannot be uninitialized. This code would throw an error:
const firstName;
(you declared it, but didn’t initialize it).
components of a for loop
4 components
for (iterator; condition; iteration statement) {
logic;
}
1. iterator - the thing (variable) you’re counting - you just declare it here.
2. condition - tells the loop how many times it should iterate. When the condition becomes false, the loop will stop.
3. Your iteration statement will tell your loop what to do with the iterator after each run.
4. Logic - what you want the program to do each time the loop runs.
What does this do:
for (let i = 0; i < count; i = i + 1) {
console.log(i)
}
as long as i is less than the amount in the “count” variable, the value of I will be printed to the console and then 1 will be added to it.
How does a for . . . of loop work in JavaScript
for (const row of rows) {
}
Where “row” is an item inside “rows” which is an array.
This type of loop iterates over each item in an iterable object (like an array) and temporarily assigns it to a variable.
sum=0
for (const score of scores) {
sum += score; (sum=sum+score)
}
this adds all the items in an array together
What is concatenation?
When you add a new string to an existing string.
let hello = “Hello”;
hello = hello + “ World”; you’re adding the string “World” to the existing string stored in the hello variable. This is called concatenation.
now hello = “Hello World”
.repeat()
.repeat() method is used on strings.
accepts a number as an argument, specifying the number of times to repeat the target string.
For example:
const activity = “Code! “;
activity.repeat(3);
produces: “Code! Code! Code!”
how to create a function in JavaScript
function buba(parameter){
return “functions are cool!”
}
The function keyword tells JavaScript that “buba” is going to be a function. “parameter” represents a value/variable that is passed into the function when it is used.
A function may have as many, or as few, parameters as you’d like.
By default, functions return undefined as their value. In order to return something else, you need to use the return keyword.
How do you call a function?
The syntax for a function call is the function name followed by parentheses. And inside the parentheses you put any necessary parameters.
For example,
padRow(8) – calls the padRow function and passes “8” as the parameter.
What is an “argument”
When you pass a value to a function call, that value is referred to as an argument - so when you pass a value as a parameter, that is an argument.
functionName(“bubba”);
In this case “bubba” is the argument.
Global Scope
- Where a variable is declared determines where in your code it can be used.
- Variables that are declared outside of any “block” like a function or
loop are in the global scope. - When a variable is in the global scope, you can use it in a functions logic. Or in other words, a function can access global variables so you can use global variables in function definitions.
const title = “Professor “;
function demo(name) {
return title + name;
}
Because “title” is a global variable, you can use it inside the function logic.