-
3일차 공부 내용JavaScript 2021. 7. 19. 02:22
1. Arrays
Array contents have a natural order, and keys are numeric and sequential.
Arrays in JavaScript have the following properties:
- Array size is not fixed; you can add or remove elements at any time.
- Arrays are not homogeneous; each individual element can be of any type.
- Arrays are zero-based. That is, the first element in the array is element 0.
To create an array literal in JavaScript, use square brackets, with the elements of the
array separated by commas:const a1 = [1, 2, 3, 4]; // array containing numbers const a2 = [1, 'two', 3, null]; // array containing mixed types const a3 = [ // array on multiple lines "What the hammer? What the chain?", "In what furnace was thy brain?", "What the anvil? What dread grasp", "Dare its deadly terrors clasp?", ]; const a4 = [ // array containing objects { name: "Ruby", hardness: 9 }, { name: "Diamond", hardness: 10 }, { name: "Topaz", hardness: 8 }, ]; const a5 = [ // array containing arrays [1, 3, 5], [2, 4, 6], ];
2. Dates and Times
the built-in Date object.
To create a date that’s initialized to the current date and time
const now = new Date();
To create a date that’s initialized to a specific date (at 12:00 a.m.):
const halloween = new Date(2021, 9, 31); // note that months are // zero-based: 9=October
To create a date that’s initialized to a specific date and time:
const halloweenParty = new Date(2021, 9, 31, 19, 0); // 19:00 = 7:00 pm
3. Regular Expressions
A regular expression (or regex or regexp) is something of a sublanguage of JavaScript.
it represents a compact way to perform complex search and replace operations on strings.
Regular expressions in JavaScript are represented by the RegExp object, and they have a literal syntax consisting of symbols between a pair of forward slashes.
// extremely simple email recognizer const email = /\b[a-z0-9._-]+@[a-z_-]+(?:\.[a-z]+)+\b/; // US phone number recognizer const phone = /(:?\+1)?(:?\(\d{3}\)\s?|\d{3}[\s-]?)\d{3}[\s-]?\d{4}/;
4. Maps and Sets
ES6 introduces the data types Map and Set, and their “weak” counterparts, WeakMap and WeakSet.
5. Data Type Conversion
5.1 Converting to Numbers
const numStr = "33.3"; const num = Number(numStr); // this creates a number value, *not* // an instance of the Number object
If the string can’t be converted to a number, NaN will be returned.
const a = parseInt("16 volts", 10); // the " volts" is ignored, 16 is // parsed in base 10 const b = parseInt("3a", 16); // parse hexadecimal 3a; result is 58 const c = parseFloat("15.5 kph"); // the " kph" is ignored; parseFloat // always assumes base 10
A Date object can be converted to a number that represents the number of milliseconds since midnight, January 1, 1970, UTC, using its valueOf() method:
const d = new Date(); // current date const ts = d.valueOf(); // numeric value: milliseconds since // midnight, January 1, 1970 UTC
5.2 Converting to String
All objects in JavaScript have a method toString().
const n = 33.5; n; // 33.5 - a number const s = n.toString(); s; // "33.5" - a string const arr = [1, true, "hello"]; arr.toString(); // "1,true,hello"
'JavaScript' 카테고리의 다른 글
4일차 공부내용 2 (0) 2021.07.20 4일차 공부내용 (0) 2021.07.20 2일차 공부 내용 2 (0) 2021.07.18 2일차 공부 내용 (0) 2021.07.18 1일차 공부 내용 (0) 2021.07.17