Javascript Flashcards
What keyword should you not forget when assigning a variable’s value for the first time?
var
Which logical operator should I use when comparing values and why or why not?
the === or !== is best because it compares the value and the type where as the == or != compares and performs a type conversion
What are considered false values
undefined, null, 0, false, NaN, ‘ ‘ (empty string)
What should you always use to terminate your line?
semi-colon ;
What is an object constructor function?
What does the ‘this’ keyword do?
Javascripts way to create an "object type" or blueprints/classes. An example would be: function Person(first, last, age, eye) { this.firstname = first; this.lastname = last; this.age = age; this.eyeColor = eye; } The this is the object itself and it 'owns' the code. In other words when a new Person is instantiated, the this will become the new objects. var myFather = new Person('John', 'Smith', '35', 'blue');
what does typeof return?
It returns the datatype
console. log(typeof 42);
output: number
console. log(typeof arr);
output: object
How do you add a new property to an existing object?
myFather.nationality = “Irish”;
How do you add a method to an object?
myFather.name = function () {
return this.firstname + “ “ + this.lastName;
};
this method will be added to myFather only
How do you add a method to a Constructor?
function Person(first, last, age, eye) { this.firstname = first; this.lastname = last; this.age = age; this.eyeColor = eye; this.name = function () { return this.firstname + " " + this.lastName;}; }
What are Javascript’s built-in constructors?
var x1 = new Object(); // A new Object object var x2 = new String(); // A new String object var x3 = new Number(); // A new Number object var x4 = new Boolean(); // A new Boolean object var x5 = new Array(); // A new Array object var x6 = new RegExp(); // A new RegExp object var x7 = new Function(); // A new Function object var x8 = new Date(); // A new Date object
What are Javascript literals?
JS primitive values that make writing code faster.
Use object literals {} instead of new Object(). Use string literals "" instead of new String(). Use number literals 12345 instead of new Number(). Use boolean literals true / false instead of new Boolean(). Use array literals [] instead of new Array(). Use pattern literals /()/ instead of new RegExp(). Use function expressions () {} instead of new Function().
What is an anonymous function?
A function that executes automatically when you create it. aka Self-Invoking function. Example: (function () { var x = "Hello"; //It will invoke itself without being called })();
How do you get a random item from an array?
var items = [1,2,3,4,5] var randomItem = items[Math.floor(Math.random() * items.length)]
How does the function math.floor() work?
it returns the largest integer less than or equal to a given number
How does the function math.random() work?
it returns a floating-point number in the range of 0-1 (not including 1)
What is return?
when evoked it is a value that is returned to the caller when the function finishes executing
What is a caller?
a caller inserts values (whether it is from the function’s argument or return) between the { } when it executes the function. Once executed, the value is copied in and are available like any other variable.
What is a parameter?
a value that is passed through a function so that the function can behave differently depending on the value that is passed. e.g.: function greetSomeone(person) { } person is the paramenter
What is an argument?
argument is the value that is passed through the function to invoke it. e.g.:
greetSomeone(‘Maika’) Maika is the argument
What is a string?
a sequence of characters
What is a boolean?
a true/false value
What is the logical operator for and, or and not?
&&, ||, !
What are array methods for javascript?
arr. push (adds a value to the end of the array)
arr. pop (removes and returns the value in an array)
arr. length (gives you the length of the array)
How do you change the value or call the value of an array?
arr[1] = 3
arr[1]