Professional Documents
Culture Documents
This is a glossary of terms and topics in JavaScript as covered in Codecademy courses. It does not attempt to completely define and explain terms, but rather provides a general overview that is appropriate for someone new to the language. For a more comprehensive treatment of these topics, we recommend the Mozilla Developer Network JavaScript documentation. Variables Programming languages use variables to store values. Variables may have global or local function scope. Variable names are case sensitive and must start with a letter, underscore (_), or a dollar sign ($). Variable Assignment Syntax
var name = value;
var name value
Is the keyword that defines a variable. Is the name of the variable. Is any JavaScript value or a reference to a value (another variable).
Example
var x = 1; var myName = "Bob"; var hisName = myName;
Numbers *, /, -, + (Multiplication, Division, Addition, Subtraction) Notes These will work as you would expect. % (Modulus) Syntax
number1 % number2
number1,2 %
Is two number variables or actual numbers. Is the modulus operator. It returns the remainder of dividing number1 by number2.
Example
14 % 9 // returns 5
++ (Increment)
variable1++
++variable2
postfix increment prefix increment
variable1 is incremented (i.e. variable1 = variable1 + 1). When evaluated, the old value of variable1 is returned. variable2 is incremented (i.e. variable1 = variable1 + 1) and its new value is returned.
-- (Decrement)
variable1---variable2
postfix increment prefix increment
variable1 is decremented (i.e. variable1 = variable1 - 1). When evaluated, the old value of variable1 is returned. variable2 is decremented (i.e. variable2 = variable2 - 1) and its new value is returned.
Syntax
Two string variables or literals to be concatenated. The concatenation operator concatenates string1 and string2 and returns a new string that is the union of both strings. Concatenate is just a fancy word which means to combine to the strings together.
Example
"some" + "text"; // returns "sometext" var first = "my"; var second = "string"; var union = first + second; // union variable has the string "mystring"
replace Returns a string with the first match substring replaced with a new substring. Example
"original string".replace("original", "replaced"); // returns "replaced string"
toUpperCase, toLowerCase Changes the cases of all the alphabetical letters in the string. Example
"my name".toUpperCase(); // Returns "MY NAME" "MY NAME".toLowerCase(); // Returns "my name"
substring Returns the sequence of characters between two indices within a string. Example
"adventures".substring(2,9); // Returns "venture"
false
Logical AND, returns true if both expression1 and expression2 are true. Otherwise it returns false. Logical OR, returns true if expression3 or expression4 are true, or both are true. Otherwise it returns false. Logical negation, returns the boolean that is the opposite of expression5.
Comparison operators
Operator
=== (Equal)
Description
!== (Not equal) > (Greater than) >= (Greater than or equal)
Returns true if the operands are not equal. Returns true if the first operand is greater than the second operand.
Returns true if the first operand is greater than or equal to the second operand. 1 >= 1; Returns true if the first operand is less than the second operand.
1 < 2; 100 < 1,000; 1 <= 2;
Returns true if the first operand is less than or equal to the second operand. 1 <= 1;
Comments A comment is generally used within code to explain how something works or to write necessary information about a section. Comments are very useful to make code more readable. Single Line Comment Syntax
// This is a single line comment.
Anything on the line following // will be a comment while anything before will still be code. Multi-Line Comment Syntax
/*Comment1 Comment2 . . Comment3 */
Comment1-N
Everything between /* and */ will be comments, which can span multiple lines.
statement2; . . . statementN; };
name parameter statement
The function's name. The variable name that will be given to an argument passed to the function. A single Javascript statement. For example, var x = 1; is a statement.
Example
var greeting = function (name) { console.log("Hello " + name); };
The name of the function to be called. A value or a reference to a value passed to a function.
Example
greeting('Hello');
condition statement1...N
A boolean condition The set of statements to execute if the condition evaluates to true.
Example
if (answer === 42) { console.log('Told you so!'); }
Notes If the condition is true, statement1 will be executed. Otherwise, statement2 will be executed. Example
if (name.length > 0) { console.log("Please enter your name."); } else { console.log("Hello " + name); }
else if Syntax
if (condition1) { statement1; } else if (condition2) { statement2; } else { statement3; }
Notes If condition1 is true, then statement1 will be executed. If condition1 is false, and condition2 is true, then statement2 will be executed. If condition1 and condition2 are both false, then statement3 will be executed. Example
if (someNumber > 10) { console.log("Numbers larger than 10 are not allowed."); } else if (someNumber < 0) { console.log("Negative numbers are not allowed."); } else { console.log("Nice number!"); }
One or more Javascript statement. A statement that yields a value - for example, 1 + 1 or x, where x is a predefined variable. A value or an expression to match against the expression
Notes Switch statements are used to check for different values of a variable (or an expression) to control the flow of the program. Example
switch (GPA){ case 90: letterGrade = "A+"; break; case 80: letterGrade = "A"; break; case 70: letterGrade = "C"; break; case 60: letterGrade = "D"; break; case 50: letterGrade = "E"; break; default: letterGrade = "F"; break; }
A boolean expression.
expression
Notes The ternary operator is usually used as a shortcut for the if statement. Example
// Using an if statement. var msg = "You "; if (grade > 50) { msg = msg + "Passed!"; } else { msg = msg + "Failed!"; } console.log(msg);
Objects An object is a list-like construct that has properties which corresponds to JavaScript values, variables, or other objects. Object Literals Syntax
{ "property 1": value1, property2: value2, number: value3 }
property1 property2 number value1,2,3
A string property of the object. This can be any valid JavaScript string. This is used when the property contains a space or a character not suitable for an identifier. An identifier (Any valid JavaScript variable name, see Variables) property. A valid integer. This can be any valid JavaScript value (number, string, boolean, object, function, etc.).
Example
var obj = {
name: "Bob", married: true, "mother's name": "Alice", "year of birth": 1987, getAge: function () { return 2012 - obj["year of birth"]; }, 1: 'one' };
The object variable name. Any valid string or a variable referencing a string value. An identifier (Any valid JavaScript variable name, see Variables).
// 'Bob' // 'Bob'
Example
obj['name']; obj.name;
obj.getAge(); // 24
An expression that is executed before the loop starts. Used to initialize a counter variable such
as i. An expression that is executed before each loop iteration. If the result is true the loop will continue otherwise it will stop. An expression used to change the counter variable at the end of each loop (often a postfix increment). Javascript statements to be executed while the condition is true.
Example
for (var time = 10; time > 10; time--) { console.log("Launching rocket in T minus " + time); console.log("DON'T PANIC"); }
An expression that is executed before each loop iteration. If the result is true, the loop will continue. Otherwise it will stop. statement1...N Javascript statements to be executed in the body of the loop while the condition is true.
condition
Example
while (isDoorOpen()) { console.log("Please close the door"); }
Do While loops A Do-While loop is very similar to a regular while loop. A Do-While loop guarantees that the code block will at least be run once regardless of the condition.
Syntax Syntax
do { statement1; statement2; . . . statementN; } while (condition);
statement1... N condition
Javascript statements to be executed while the condition is true. An expression that is executed during each loop iteration. If the result is true, the loop will continue. Otherwise, it will stop. The condition is only checked after the first run loop.
Example
do { console.log("Runs at least once"); } while (false)
Arrays An array is a special type of object and is an ordered list of JavaScript values like numbers, strings or even objects. Array Literals Syntax
[value1, value2, ..., valueN]
value1...N
Example
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
The array variable name. The 0-based index of the element in the array. Note that the position of an element in an array is
determined by the number of elements to the left of it. As a result, the first element in an array is at index 0.
Example
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]; primes[0]; // 2 primes[3]; // 7
Visual: [ | | | | | ]
Visual: [ | | | | | ] [ | | | | | ] [ | | | | | ] [ | | | | | ]
Math Math is an object that contains a library of math functions. random Syntax
Math.random()
floor Syntax
Math.floor(expression)
Example
Math.floor(9.99); // 9 Math.floor(1 + 0.5); // 1 Math.floor(Math.random() * X + 1); // Returns a random number between 1 and X
Errors
A valid Javascript keyword/character is var x - 1; found in a place in Syntax Error: your code where a SyntaxError: Unexpected Unexpected Token different token keyword/character is expected. A non-keyword was var x a 1 found in a place in Syntax Error: your code where a SyntaxError: Unexpected Unexpected certain identifier keyword/character is identifier a expected. This happens when 1 = x ReferenceError: we try to assign a Invalid left-hand value to a nonReferenceError: Invalid left-hand side in assignment side in assignment variable. This happens when a++; ReferenceError: we try to use a {word} is not variable which has ReferenceError: a is not defined defined not been defined.
Example
alert("Hello World");
prompt Syntax
prompt(message);
message
Example
var name = prompt("Enter your name:");
confirm Syntax
confirm(message);
message
OOP Classes A class can be thought of as a template to create many objects with similar qualities. Classes are a fundamental component of object-oriented programming (OOP). Class constructor Syntax
var ClassName = function (parameter1, parameter2, ..., parameterN) { this.property1 = parameter1; this.property2 = expression; };
The name of the new class. By convention, class names are always uppercase. Parameters passed in when the class is created. This is generally used to assign a property value. Parameters are optional. Refers to the class instance object being created. A property or "attribute" of the class. This can be either an simple value or a function. Can be initialized with default values or values passed in through a parameter. An example of a property that has a default value (in this case its the result of an expression) that is not the value of a parameter.
Example
var Vegetable = function (color) { this.color = color; this.delicious = true; this.eat = function() { console.log("nom"); }; };
New instances of the ClassName class. These are objects. The class we are creating an instance of. Arguments we are passing into the class.
Example
var broccoli = new Vegetable("green"); console.log("Broccoli is " + broccoli.color + "!"); // will print "Broccoli is green!"
Notes In the example above, we are accessing the color property on the object "broccoli" that is an instance of the class "Vegetable". See object property access. Class methods Syntax
ClassName.prototype.methodName = function (paramater1, paramater2, ... paramaterN) { statement1 .
. statmentN };
ClassName methodName paramater1...N
The class which the method is being added to. The name of the method we're adding. The method parameters.
Example
var Person = function () { this.job = "Unemployed"; };
Inheritance Inheritance allows one class to gain properties from another class. Syntax
SubClass.prototype = new SuperClass();
SubClass SuperClass
The class that receives the inheritance, the child. The class that gives the inheritance, the parent.
Example
var PoliceOfficer = function (age) { this.job = "Police Officer"; this.age = age; };
PoliceOfficer.prototype.retire = function () { if (this.age > 66) { this.retired = true; } else { this.retired = false; }
return this.retired; };
Example
var Lieutenant = function (age) { this.rank = "Lieutenant"; this.age = age; };
John.retire(); // true