Basic JavaScript Flashcards
Declare variables of type number, string, boolean, and undefined. (Not constants)
let someNum = 42;
let username = “playerOne”;
let isThisBool = true;
let iDunno = undefined;
Declare a constant of type string for username.
const username = “playerOne”;
Declare two numbers variables on the same line.
let numOne = 41, numTwo = 42;
A single line comment
// Single line of comment code
Multiple line comment
/* Multi
Line
Comment */
Increment and decrement a number variable by one.
someNumber++;
someNumber–;
Print a string to the browser console.
console.log(“Some String”);
Access a specific character from a string.
someString = “All these words:;
someCharacter = someString[0]; //First character
Basic conditional statement: if num is greater than 0, print a message to the console, else print another message.
if (num > 0) {
console.log(“Greater than zero”);
} else if (num =< 0) {
console.log(“NOT Greater than zero”);
}
Check if numOne and numTwo are STRICTLY equal.
if (numOne === numTwo){
console.log(“Yeah, it is”);
}
JavaScript syntax for AND, OR, Not Equal
&& (true && false)
|| (true || false)
! true
Switch (case) statement to set the “creator” variable based on the value of the “os” variable.
let creator;
switch (os) {
case “linux”:
creator = “Linus Torvalds”;
break;
case “windows”:
creator = “Bill Gates”;
break;
case “mac”:
creator = “Steve”;
break;
default:
creator = “Unknown”;
break;
}
Using a Ternary operator, set a variable’s value based on a condition.
const price = isMember ? “$2.00” : “$10.00”;
*After the question mark it the True value followed by the False value.
Using Nullish Coalescence, check for a name variable to be null, set a value accordingly.
let myName = null;
someName = myName ?? “Anonymous”;
Create a basic function that multiplies two numbers.
function multiplyTwoNums(numOne, numTwo){
let multi = numOne * numTwo;
return multi;
}
Anonymous function
// using an anonymous function
conversions(
function (a) {
return a + a;
},
1,
2,
3,
);
// 2 4 6
Define an immediately invoked function expression (IIFE) that declares a variable of result equal to the sum of a + b.
const result = (function (a, b) { return a + b; })(1, 2);
console.log(result); // 3