Javascript Basics Flashcards
means to “commented out”
// on the line first line
the “.” is a “full stop” and the length give you the length of the word
“word”.length
This will make a pop up box that you have to click to continue
confirm(‘some sentence here’);
an input the the program needs to take an action
prompt(“What is your name?”);
strings are written with quotes
“what is your name?” or “42”
booleans are used to compare numbers and strings
true or false
this determines if this is string is greater than 10 characters and returns the result “true”
“I’m coding like a champ!”.length > 10;
whatever is in the parentheses is “logged to the console” (sometimes called “printing out”), or displaying what the computer is thinking/doing
console.log(2 * 5)
standard operators
> ,=
equal to
===
Not equal to
!==
If statement that if true, will “log the string in the console”
if ( “Brent”.length < 7 ) {
console.log(“Wonderfully True”);
}
Modulo - When % is placed between two numbers, the computer will divide the first number by the second, and then return the remainder of that division.
%
Substring - x is where you start chopping, and y is where you stop chopping each character is numbered starting at 0
“some word”.substring(x, y)
var varName = data;
once you “declare” a variable name, you can call up that value by declaring the variable nameusing a variable, you don’t put it in parenthesis because it will turn it into a string, rather than using the variable value (which can be a string or a number)
this will ask the user for a response, and store the response as the value of the variable “age”
var age = prompt(“What is your age?”);
Function structure
var divideByThree = function (number) { var val = number / 3; console.log(val); }; divideByThree(12);
Parts of a function
the code between the { } is what is run every time the function is calledInputOutputCallThe code in the (parentheses) is parameterit’s a placeholder word that we give a specific value when we call the function. Parameters go in the parentheses. The computer will look out for it in the code block.
D.R.Y.
don’t repeat yourself.
return structure
var timesTwo = function(number) { return number * 2; }
about return
return saves the number to the machine so that it can be used in other code, like this:
var newNumber = timesTwo(6) { console.log(newNumber); }
note you do not need to define a second variable to do some calculation and then return the variable, it all can be calculated with the return on one line
only valid inside functions!
scope global
Variables defined outside a function are accessible anywhere once they have been declared. They are called global variables and their scope is global
scope local
Variables defined inside a function are local variables. They cannot be accessed outside of that function.
Random number
Math.random()
If we declare a variable and make it equal to Math.random(), that variable will equal a number between 0 and 1.