Karteikarten
Javascript
Karten in diesem Stapel (49)
- 1return character at a certain index string = "Hello";charAt();
example: string.charAt(0);
returns "H"
JS global variable - 2returns the index of first occurrence of a string string = "Hello"indexOf();
example: string.indexOf("H");
returns 0 (-1 if no match) - 3returns a specified part of a string between two indexes string = "Hello"slice();
string.slice(1); returns "H"
or string.slice(1,3); returns "He"
note that it only goes up to the last arg, not including it - 4concatenate/join two strings to another string1 = "Hello" string2 = "today"concat();
string1 = string1.concat(" ", string2);
returns "Hello today"; - 5check if a string ends with a specified stringendsWith();
string.endsWith("Deb"); - 6check if a string contains a substringincludes();
string.includes("Hello"); - 7check if a regexp is in a string and return first or each of the matches in an arraymatch();
string.match(/\nHello/g);
returns [Hello,Hello,Hello] if there are 3
use /g to return all of them - 8repeat a string the specified number of timesrepeat();
string.repeat(2);
returns "HelloHello" - 9breaks a string into an array of substringssplit();
string.split("e");
strings are split by whatever is the arg
returns ["H","llo"] - 10convert to a string object var foo = 3;toString();
foo.toString();
returns "3" - 11convert to uppercase and to lowercasetoUpperCase();
toLowerCase(); - 12remove whitespace from both ends of a stringtrim();
" Hello ! "
converts to "Hello !" - 13return the number of characters in a stringlength();
for string = "Hello!"
string.length(); returns 6 - 14replace one string or substring with anotherreplace();
something.replace("Hello","Tsup");
replace the first arg with second arg - 15return the first index of the argsearch();
for string = "Hello"
string.search(/ell/); returns 1 - 16check if a number is an integernum.isInteger();
- 17converts a number to one with specified # of digits after the decimalnum.toFixed(2);
- 18converts a number to one with specified # of digits TOTALnum.toPrecision(3);
- 19check if a number is finite or not. what are infinite nums?num.isFinite();
infinite nums are repeating decimals, NaN, or infinity - 20if condition is met, then set the variable to first thing, or else set it to the last thingvarName = (condition) ? "value" : "value2"
ex: barStatement = (age > 21) ? "Have a beer!" : "No alcohol!"
known as ternary operator - 21what is truthy and falsy?they evaluate as truth or false in a boolean statement, even if they are not actual false.
- 22what falsy values are not actually false?NaN, null, undefined, 0, ""
- 23What can be an object type and what can't?everything that is not a primitive type (string, number, boolean, null/undefined).
includes arrays and hashes. - 24What does it mean that JS variables are untyped?You can assign a variable a value of one type (eg true, which is boolean), and then reassign it a value of another type (eg "hello", which is a string).
- 25What does an object have?It has a collection of properties, and each property has a name-value pair. (huh?)
- 26What are the primitive types, and how are they different from object types?String, number, boolean, null/undefined.
They have values, whereas object types have properties (which in turn have name/value pairs). - 27What are the two ways to create objects?Literal notation and constructor notation
- 28What is literal notation? What is it useful for?Puts all the properties right into the object variable declaration. It has a { after the =.
var myObject = { firstProp: ‘something’, secondProp: 40 };
Useful for one-off object creation. - 29What is constructor notation? What is this useful for?Requires the use of "new" to create the object variable, then use .notation or []notation to separately create the properties and methods.
Useful for creating many objects as if from a template. - 30return the type of a variable, object, function or expressiontypeof
"hello" typeof String - 31what can the typeof operator not do?it can't tell if something is a Date or an Array
- 32Determine is something is an instance of a larger objectinstanceOf
cars instanceOf Array - 33What can instanceOf be used for?Array, Object, String, Number, or a constructor of some kind
- 34When is a variable that is part of an object variable? What forms does it come in?Property
Key/value or name/value - 35What is a function that is a part of an object variable?Method
- 36What is the difference between key/value and name/value?They mean essentially the same thing.
Name/value is with = signs.
Key/value is with : signs. - 37Example of creating an object with literal notation?var hotel = {
name: 'Quay', rooms: 40, checkAvailability: function(){ dosomething;}
} - 38Example of constructor notation?function Hotel(name, rooms, booked){ this.name = name;
this.rooms = rooms;
this.booked = booked;
checkAvailability = function(){
dosomething ;
};
}
create a new object like: new Hotel(‘Deb Hotel’, 40, 22); - 39How can you tell if something is a constructor and not a normal function?It is capitalized.
- 40How can you access an object's properties and methods?Use dot notation (preferred) or bracket notation.
- 41Example of dot notation and what it's used for?var hotelName = hotel.name;
used to access an object's properties - 42Example of bracket notation and what it's used for?var hotelName = hotel['name'];
- 43Example of arrays in an object?costsObject = { room1:[42,23,3], room2:[52,62,5] };
- 44Example of objects in an array?costsArray = [ {accom: 420, food: 40, phone: 10}, {accom:520, food:42, phone:20} ];
- 45Example of how you can you access the values of arrays in an object?costsObject.room2[0];
Given:
costsObject = { room1:[42,23,3], room2:[52,62,5] };
Returns 52 - 46Example of how you can acccess the values of objects in an array:costsArray[0].food;
Given:
costsArray = [ {accom: 420, food: 40, phone: 10}, {accom:520, food:42, phone:20} ];
returns 40 - 47What is the difference between null and undefined?Technically they can be used interchangeably, but null tends to mean the absence of something and undefined refers more to errors (and / by 0).
- 48Why is JS terrible at calculations?It uses binary-floating point numbers, which approximates values but doesn't always get them right due to rounding erros (so .3 - .2 doesn't equal exactly .1).
- 49What is a workaround for the binary-floating point number problem in JS?Using scaled integers (?)
Informationen
- Beschreibung
- Karteikarten am Javascript, erstellt von D F am 21/11/2017.
- Erstellt von
- D F
- Aufrufe
- 0
- Bewertung