├── JavaScript ├── string │ ├── string-fromcodepoint.js │ ├── string-fromcharcode.js │ ├── string-codepointat.js │ ├── string-substr.js │ ├── string-substring.js │ ├── string-length.js │ ├── string-tolowercase.js │ ├── string-tostring.js │ ├── string-touppercase.js │ ├── string-valueof.js │ ├── string-startswith.js │ ├── string-trim.js │ ├── string-trimend.js │ ├── string-trimstart.js │ ├── string-match.js │ ├── string-concat.js │ ├── string-tolocaleuppercase.js │ ├── string-padend.js │ ├── string-charat.js │ ├── string-endswith.js │ ├── string-tolocalelowercase.js │ ├── string-includes.js │ ├── string-localecompare.js │ ├── string-charcodeat.js │ ├── string-matchall.js │ ├── string-raw.js │ ├── string-lastindexof.js │ ├── string-padstart.js │ ├── string-search.js │ ├── string-slice.js │ ├── string-iterator.js │ ├── string-split.js │ ├── string-replace.js │ ├── string-indexof.js │ ├── string-normalize.js │ └── string-repeat.js ├── boolean │ ├── boolean-constructor.js │ ├── boolean-valueof.js │ ├── boolean-tostring.js │ └── meta.json ├── array │ ├── array-tostring.js │ ├── array-length.js │ ├── array-find.js │ ├── array-concat.js │ ├── array-from.js │ ├── array-map.js │ ├── array-unshift.js │ ├── array-findindex.js │ ├── array-foreach.js │ ├── array-every.js │ ├── array-some.js │ ├── array-shift.js │ ├── array-lastindexof.js │ ├── array-reduce-right.js │ ├── array-keys.js │ ├── array-values.js │ ├── array-filter.js │ ├── array-entries.js │ ├── array-join.js │ ├── array-sort.js │ ├── array-includes.js │ ├── array-tolocalestring.js │ ├── array-reduce.js │ ├── array-indexof.js │ ├── array-copywithin.js │ ├── array-fill.js │ ├── array-slice.js │ ├── array-pop.js │ ├── array-splice.js │ ├── array-push.js │ └── array-reverse.js ├── statement │ ├── statement-while.js │ ├── statement-for.js │ ├── statement-function.js │ ├── statement-break.js │ ├── statement-class.js │ ├── statement-let.js │ ├── statement-var.js │ ├── statement-dowhile.js │ ├── statement-forof.js │ ├── statement-block.js │ ├── statement-continue.js │ ├── statement-empty.js │ ├── statement-ifelse.js │ ├── statement-label.js │ ├── statement-forin.js │ ├── statement-trycatch.js │ ├── statement-functionasterisk.js │ ├── statement-return.js │ ├── statement-throw.js │ ├── statement-const.js │ ├── statement-default.js │ ├── statement-async.js │ ├── statement-switch.js │ ├── statement-async-function-asterisk.js │ └── statement-async-for-in.js ├── date │ ├── date-gethours.js │ ├── date-getminutes.js │ ├── date-getseconds.js │ ├── date-getfullyear.js │ ├── date-getmonth.js │ ├── date-getutcseconds.js │ ├── date-getdate.js │ ├── date-toutcstring.js │ ├── date-getday.js │ ├── date-getmilliseconds.js │ ├── date-totimestring.js │ ├── date-getutcmilliseconds.js │ ├── date-tostring.js │ ├── date-gettime.js │ ├── date-setutcdate.js │ ├── date-setmilliseconds.js │ ├── date-setutcminutes.js │ ├── date-setutcseconds.js │ ├── date-setutcmilliseconds.js │ ├── date-valueof.js │ ├── date-getutcdate.js │ ├── date-parse.js │ ├── date-setfullyear.js │ ├── date-setmonth.js │ ├── date-getutchours.js │ ├── date-setdate.js │ ├── date-setseconds.js │ ├── date-todatestring.js │ ├── date-toisostring.js │ ├── date-tojson.js │ ├── date-getutcfullyear.js │ ├── date-setminutes.js │ ├── date-getutcday.js │ ├── date-utc.js │ ├── date-getutcmonth.js │ ├── date-getutcminutes.js │ ├── date-constructor.js │ ├── date-settime.js │ ├── date-sethours.js │ ├── date-setutcmonth.js │ ├── date-now.js │ ├── date-setutchours.js │ ├── date-setutcfullyear.js │ ├── date-tolocaletimestring.js │ ├── date-gettimezoneoffset.js │ ├── date-tolocalestring.js │ └── date-tolocaledatestring.js ├── function │ ├── function-constructor.js │ ├── function-length.js │ ├── function-name.js │ ├── function-apply.js │ ├── function-call.js │ ├── function-tostring.js │ └── function-bind.js ├── math │ ├── math-ln2.js │ ├── math-log2e.js │ ├── math-sqrt2.js │ ├── math-ln10.js │ ├── math-log10e.js │ ├── math-sqrt1_2.js │ ├── math-max.js │ ├── math-min.js │ ├── math-pi.js │ ├── math-sign.js │ ├── math-ceil.js │ ├── math-floor.js │ ├── math-log.js │ ├── math-cbrt.js │ ├── math-log2.js │ ├── math-round.js │ ├── math-trunc.js │ ├── math-log10.js │ ├── math-log1p.js │ ├── math-hypot.js │ ├── math-imul.js │ ├── math-tanh.js │ ├── math-abs.js │ ├── math-acosh.js │ ├── math-asinh.js │ ├── math-atanh.js │ ├── math-expm1.js │ ├── math-fround.js │ ├── math-sinh.js │ ├── math-sqrt.js │ ├── math-exp.js │ ├── math-acos.js │ ├── math-asin.js │ ├── math-atan2.js │ ├── math-clz32.js │ ├── math-pow.js │ ├── math-atan.js │ ├── math-random.js │ ├── math-cosh.js │ ├── math-cos.js │ ├── math-tan.js │ ├── math-sin.js │ └── math-e.js ├── map │ ├── map-prototype-@@tostringtag.js │ ├── map-prototype-size.js │ ├── map-prototype-has.js │ ├── map-prototype-get.js │ ├── map-prototype-set.js │ ├── map-prototype-clear.js │ ├── map-prototype-delete.js │ ├── map-prototype-keys.js │ ├── map-prototype-values.js │ ├── map-prototype-entries.js │ ├── map-prototype-@@iterator.js │ └── map-prototype-foreach.js ├── functions │ ├── functions-definitions.js │ ├── functions-default.js │ ├── functions-arrow.js │ ├── functions-setter.js │ ├── functions-arguments.js │ ├── functions-getter.js │ └── functions-restparameters.js ├── promise │ ├── promise-resolve.js │ ├── promise-catch.js │ ├── promise-then.js │ ├── promise-reject.js │ ├── promise-all.js │ ├── promise-constructor.js │ ├── promise-allsettled.js │ └── promise-race.js ├── typedarray │ ├── typedarray-reverse.js │ ├── typedarray-from.js │ ├── typedarray-name.js │ ├── typedarray-map.js │ ├── typedarray-sort.js │ ├── typedarray-bytes-per-element.js │ ├── typedarray-slice.js │ ├── typedarray-of.js │ ├── typedarray-tostring.js │ ├── typedarray-keys.js │ ├── typedarray-fill.js │ ├── typedarray-length.js │ ├── typedarray-bytelength.js │ ├── typedarray-entries.js │ ├── typedarray-values.js │ ├── typedarray-buffer.js │ ├── typedarray-lastindexof.js │ ├── typedarray-every.js │ ├── typedarray-find.js │ ├── typedarray-findindex.js │ ├── typedarray-reduce.js │ ├── typedarray-includes.js │ ├── typedarray-subarray.js │ ├── typedarray-copywithin.js │ ├── typedarray-filter.js │ ├── typedarray-join.js │ ├── typedarray-indexof.js │ ├── typedarray-set.js │ ├── typedarray-some.js │ └── typedarray-constructor.js ├── bigint │ ├── bigint-valueof.js │ ├── bigint-tostring.js │ ├── bigint-tolocalestring.js │ ├── bigint-asintn.js │ ├── bigint-asuintn.js │ └── meta.json ├── object │ ├── object-keys.js │ ├── object-getownpropertynames.js │ ├── object-getprototypeof.js │ ├── object-values.js │ ├── object-freeze.js │ ├── object-fromentries.js │ ├── object-isextensible.js │ ├── object-issealed.js │ ├── object-isfrozen.js │ ├── object-defineproperties.js │ ├── object-prototype-valueof.js │ ├── object-entries.js │ ├── object-prototype-tostring.js │ ├── object-defineproperty.js │ ├── object-getownpropertydescriptor.js │ ├── object-prototype-tolocalestring.js │ ├── object-assign.js │ ├── object-getownpropertydescriptors.js │ ├── object-getownpropertysymbols.js │ ├── object-prototype-seal.js │ ├── object-preventextensions.js │ ├── object-prototype-hasownproperty.js │ ├── object-prototype-isprototypeof.js │ ├── object-prototype-propertyisenumerable.js │ └── object-create.js ├── arraybuffer │ ├── arraybuffer-constructor.js │ ├── arraybuffer-isview.js │ ├── arraybuffer-bytelength.js │ ├── arraybuffer-slice.js │ └── meta.json ├── expressions │ ├── expressions-functionexpression.js │ ├── expressions-this.js │ ├── expressions-commaoperators.js │ ├── expressions-yieldasterisk.js │ ├── expressions-assignment.js │ ├── expressions-newtarget.js │ ├── expressions-operatorprecedence.js │ ├── expressions-functionasteriskexpression.js │ ├── expressions-spreadsyntax.js │ ├── expressions-voidoperator.js │ ├── expressions-logicaloperator.js │ ├── expressions-newoperator.js │ ├── expressions-comparisonoperators.js │ ├── expressions-deleteoperator.js │ ├── expressions-yield.js │ ├── expressions-objectinitializer.js │ ├── expressions-destructuringassignment.js │ ├── expressions-classexpression.js │ ├── expressions-groupingoperator.js │ ├── expressions-inoperator.js │ ├── expressions-conditionaloperators.js │ ├── expressions-typeof.js │ ├── expressions-propertyaccessors.js │ ├── expressions-arithmetic.js │ ├── expressions-instanceof.js │ └── expressions-bitwiseoperators.js ├── globalprops │ ├── globalprops-globalthis.js │ ├── globalprops-null.js │ ├── globalprops-undefined.js │ ├── globalprops-nan.js │ ├── globalprops-encodeuricomponent.js │ ├── globalprops-infinity.js │ ├── globalprops-isnan.js │ ├── globalprops-parseint.js │ ├── globalprops-isfinite.js │ ├── globalprops-eval.js │ ├── globalprops-parsefloat.js │ ├── globalprops-decodeuricomponent.js │ ├── globalprops-decodeuri.js │ └── globalprops-encodeuri.js ├── json │ ├── json-parse.js │ ├── meta.json │ └── json-stringify.js ├── regexp │ ├── regexp-prototype-flags.js │ ├── regexp-prototype-global.js │ ├── regexp-prototype-@@replace.js │ ├── regexp-constructor.js │ ├── regexp-lastindex.js │ ├── regexp-prototype-@@search.js │ ├── regexp-getregexp-@@species.js │ ├── regexp-prototype-ignorecase.js │ ├── regexp-prototype-source.js │ ├── regexp-prototype-sticky.js │ ├── regexp-prototype-exec.js │ ├── regexp-prototype-@@match.js │ ├── regexp-prototype-unicode.js │ ├── regexp-prototype-multiline.js │ ├── regexp-prototype-@@split.js │ ├── regexp-prototype-tostring.js │ ├── regexp-prototype-@@matchall.js │ └── regexp-prototype-test.js ├── sharedarraybuffer │ ├── sharedarraybuffer-bytelength.js │ ├── sharedarraybuffer-constructor.js │ ├── sharedarraybuffer-slice.js │ └── meta.json ├── symbol │ ├── symbol-prototype.js │ ├── symbol-hasinstance.js │ ├── symbol-iterator.js │ ├── symbol-matchall.js │ ├── symbol-toprimitive.js │ ├── symbol-tostringtag.js │ ├── symbol-unscopables.js │ ├── symbol-search.js │ ├── symbol-replace.js │ ├── symbol-for.js │ ├── symbol-species.js │ ├── symbol-constructor.js │ ├── symbol-keyfor.js │ ├── symbol-prototype-description.js │ ├── symbol-split.js │ ├── symbol-prototype-tostring.js │ ├── symbol-isconcatspreadable.js │ └── symbol-match.js ├── number │ ├── number-epsilon.js │ ├── number-isfinite.js │ ├── number-nan.js │ ├── number-valueof.js │ ├── number-tostring.js │ ├── number-isinteger.js │ ├── number-isnan.js │ ├── number-max-safe-integer.js │ ├── number-min-safe-integer.js │ ├── number-toexponential.js │ ├── number-tofixed.js │ ├── number-tolocalestring.js │ ├── number-toprecision.js │ ├── number-parseint.js │ ├── number-min-value.js │ ├── number-parsefloat.js │ ├── number-issafeinteger.js │ ├── number-max-value.js │ ├── number-positive-infinity.js │ └── number-negative-infinity.js ├── classes │ ├── classes-constructor.js │ ├── classes-static.js │ ├── meta.json │ └── classes-extends.js ├── dataview │ ├── dataview-buffer.js │ ├── dataview-byteoffset.js │ ├── dataview-getfloat64.js │ ├── dataview-setfloat64.js │ ├── dataview-getfloat32.js │ ├── dataview-getint8.js │ ├── dataview-setfloat32.js │ ├── dataview-setint8.js │ ├── dataview-getuint8.js │ ├── dataview-setuint8.js │ ├── dataview-getint16.js │ ├── dataview-setint16.js │ ├── dataview-getuint16.js │ ├── dataview-setuint16.js │ ├── dataview-getint32.js │ ├── dataview-setint32.js │ ├── dataview-getuint32.js │ ├── dataview-setuint32.js │ ├── dataview-bytelength.js │ ├── dataview-constructor.js │ ├── dataview-getbigint64.js │ ├── dataview-getbiguint64.js │ ├── dataview-setbigint64.js │ └── dataview-setbiguint64.js ├── set │ ├── set-prototype-add.js │ ├── set-prototype-clear.js │ ├── set-prototype-size.js │ ├── set-prototype-has.js │ ├── set-prototype-constructor.js │ ├── set-prototype-values.js │ ├── set-prototype-entries.js │ ├── set-prototype-delete.js │ ├── set-prototype-@@iterator.js │ └── set-prototype-foreach.js ├── reflect │ ├── reflect-preventextensions.js │ ├── reflect-get.js │ ├── reflect-getprototypeof.js │ ├── reflect-set.js │ ├── reflect-ownkeys.js │ ├── reflect-has.js │ ├── reflect-construct.js │ ├── reflect-defineproperty.js │ ├── reflect-deleteproperty.js │ ├── reflect-setprototypeof.js │ ├── reflect-isextensible.js │ ├── reflect-getownpropertydescriptor.js │ └── reflect-apply.js ├── atomics │ ├── atomics-islockfree.js │ ├── atomics-store.js │ ├── atomics-add.js │ ├── atomics-sub.js │ ├── atomics-load.js │ ├── atomics-or.js │ ├── atomics-and.js │ ├── atomics-xor.js │ ├── atomics-exchange.js │ └── atomics-compareexchange.js ├── weakmap │ ├── weakmap-prototype-delete.js │ ├── weakmap-prototype-get.js │ ├── weakmap-prototype-has.js │ ├── weakmap-prototype-set.js │ └── meta.json ├── weakset │ ├── weakset-prototype-has.js │ ├── weakset-prototype-delete.js │ ├── meta.json │ └── weakset-prototype-add.js ├── intl │ ├── intl-relativetimeformat-prototype-format.js │ ├── intl-collator-supportedlocalesof.js │ ├── intl-relativetimeformat-prototype-formattoparts.js │ ├── intl-numberformat-supportedlocalesof.js │ ├── intl-datetimeformat-supportedlocalesof.js │ ├── intl-locale-prototype-tostring.js │ ├── intl-datetimeformat-prototype-resolvedoptions.js │ ├── intl-relativetimeformat-supportedlocalesof.js │ ├── intl-collator.js │ ├── intl-getcanonicallocales.js │ ├── intl-locale.js │ ├── intl-numberformat-prototype-resolvedoptions.js │ ├── intl-locale-prototype-maximize.js │ ├── intl-locale-prototype-minimize.js │ ├── intl-relativetimeformat.js │ ├── intl-collator-prototype-compare.js │ ├── intl-numberformat-prototype-format.js │ ├── intl-relativetimeformat-prototype-resolvedoptions.js │ ├── intl-numberformat.js │ ├── intl-datetimeformat.js │ ├── intl-listformat.js │ ├── intl-datetimeformat-prototype-format.js │ └── intl-datetimeformat-prototype-formatrange.js └── proxyhandler │ ├── proxyhandler-ownkeys.js │ ├── proxyhandler-construct.js │ ├── proxyhandler-getprototypeof.js │ ├── proxyhandler-getownpropertydescriptor.js │ ├── proxyhandler-preventextensions.js │ ├── proxyhandler-apply.js │ ├── proxyhandler-has.js │ ├── proxyhandler-get.js │ ├── proxyhandler-defineproperty.js │ ├── proxyhandler-deleteproperty.js │ ├── proxyhandler-setprototypeof.js │ ├── proxyhandler-set.js │ └── proxyhandler-isextensible.js └── README.md /JavaScript/string/string-fromcodepoint.js: -------------------------------------------------------------------------------- 1 | console.log(String.fromCodePoint(65, 90, 48)); 2 | // expected output: "AZ0" -------------------------------------------------------------------------------- /JavaScript/string/string-fromcharcode.js: -------------------------------------------------------------------------------- 1 | console.log(String.fromCharCode(189, 43, 190, 61)); 2 | // expected output: "½+¾=" -------------------------------------------------------------------------------- /JavaScript/string/string-codepointat.js: -------------------------------------------------------------------------------- 1 | var icons = '☃★♲'; 2 | 3 | console.log(icons.codePointAt(1)); 4 | // expected output: "9733" -------------------------------------------------------------------------------- /JavaScript/boolean/boolean-constructor.js: -------------------------------------------------------------------------------- 1 | const flag = new Boolean(); 2 | 3 | console.log(flag); 4 | // expected output: Boolean: false -------------------------------------------------------------------------------- /JavaScript/array/array-tostring.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 'a', '1a']; 2 | 3 | console.log(array1.toString()); 4 | // expected output: "1,2,a,1a" -------------------------------------------------------------------------------- /JavaScript/statement/statement-while.js: -------------------------------------------------------------------------------- 1 | var n = 0; 2 | 3 | while (n < 3) { 4 | n++; 5 | } 6 | 7 | console.log(n); 8 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/date/date-gethours.js: -------------------------------------------------------------------------------- 1 | const birthday = new Date('March 13, 08 04:20'); 2 | 3 | console.log(birthday.getHours()); 4 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/function/function-constructor.js: -------------------------------------------------------------------------------- 1 | const sum = new Function('a', 'b', 'return a + b'); 2 | 3 | console.log(sum(2, 6)); 4 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/array/array-length.js: -------------------------------------------------------------------------------- 1 | const clothing = ['shoes', 'shirts', 'socks', 'sweaters']; 2 | 3 | console.log(clothing.length); 4 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/date/date-getminutes.js: -------------------------------------------------------------------------------- 1 | const birthday = new Date('March 13, 08 04:20'); 2 | 3 | console.log(birthday.getMinutes()); 4 | // expected output: 20 -------------------------------------------------------------------------------- /JavaScript/math/math-ln2.js: -------------------------------------------------------------------------------- 1 | function getNatLog2() { 2 | return Math.LN2; 3 | } 4 | 5 | console.log(getNatLog2()); 6 | // expected output: 0.6931471805599453 -------------------------------------------------------------------------------- /JavaScript/math/math-log2e.js: -------------------------------------------------------------------------------- 1 | function getLog2e() { 2 | return Math.LOG2E; 3 | } 4 | 5 | console.log(getLog2e()); 6 | // expected output: 1.4426950408889634 -------------------------------------------------------------------------------- /JavaScript/math/math-sqrt2.js: -------------------------------------------------------------------------------- 1 | function getRoot2() { 2 | return Math.SQRT2; 3 | } 4 | 5 | console.log(getRoot2()); 6 | // expected output: 1.4142135623730951 -------------------------------------------------------------------------------- /JavaScript/date/date-getseconds.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 69 00:20:18'); 2 | 3 | console.log(moonLanding.getSeconds()); 4 | // expected output: 18 -------------------------------------------------------------------------------- /JavaScript/math/math-ln10.js: -------------------------------------------------------------------------------- 1 | function getNatLog10() { 2 | return Math.LN10; 3 | } 4 | 5 | console.log(getNatLog10()); 6 | // expected output: 2.302585092994046 -------------------------------------------------------------------------------- /JavaScript/math/math-log10e.js: -------------------------------------------------------------------------------- 1 | function getLog10e() { 2 | return Math.LOG10E; 3 | } 4 | 5 | console.log(getLog10e()); 6 | // expected output: 0.4342944819032518 -------------------------------------------------------------------------------- /JavaScript/date/date-getfullyear.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 69 00:20:18'); 2 | 3 | console.log(moonLanding.getFullYear()); 4 | // expected output: 1969 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-@@tostringtag.js: -------------------------------------------------------------------------------- 1 | var map1 = Object.prototype.toString.call(new Map()); 2 | 3 | console.log(map1); 4 | // expected output: "[object Map]" -------------------------------------------------------------------------------- /JavaScript/functions/functions-definitions.js: -------------------------------------------------------------------------------- 1 | var obj = { 2 | foo() { 3 | return 'bar'; 4 | } 5 | } 6 | 7 | console.log(obj.foo()); 8 | // expected output: "bar" -------------------------------------------------------------------------------- /JavaScript/math/math-sqrt1_2.js: -------------------------------------------------------------------------------- 1 | function getRoot1Over2() { 2 | return Math.SQRT1_2; 3 | } 4 | 5 | console.log(getRoot1Over2()); 6 | // expected output: 0.7071067811865476 -------------------------------------------------------------------------------- /JavaScript/date/date-getmonth.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 69 00:20:18'); 2 | 3 | console.log(moonLanding.getMonth()); // (January gives 0) 4 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/date/date-getutcseconds.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 1969, 20:18:04 UTC'); 2 | 3 | console.log(moonLanding.getUTCSeconds()); 4 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/promise/promise-resolve.js: -------------------------------------------------------------------------------- 1 | var promise1 = Promise.resolve(123); 2 | 3 | promise1.then(function(value) { 4 | console.log(value); 5 | // expected output: 123 6 | }); -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-reverse.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([1, 2, 3]); 2 | uint8.reverse(); 3 | 4 | console.log(uint8); 5 | // expected output: Uint8Array [3, 2, 1] -------------------------------------------------------------------------------- /JavaScript/array/array-find.js: -------------------------------------------------------------------------------- 1 | const array1 = [5, 12, 8, 130, 44]; 2 | 3 | const found = array1.find(element => element > 10); 4 | 5 | console.log(found); 6 | // expected output: 12 -------------------------------------------------------------------------------- /JavaScript/bigint/bigint-valueof.js: -------------------------------------------------------------------------------- 1 | console.log(typeof Object(1n)); 2 | // expected output: "object" 3 | 4 | console.log(typeof Object(1n).valueOf()); 5 | // expected output: "bigint" -------------------------------------------------------------------------------- /JavaScript/date/date-getdate.js: -------------------------------------------------------------------------------- 1 | const birthday = new Date('August 19, 1975 23:15:30'); 2 | const date1 = birthday.getDate(); 3 | 4 | console.log(date1); 5 | // expected output: 19 -------------------------------------------------------------------------------- /JavaScript/date/date-toutcstring.js: -------------------------------------------------------------------------------- 1 | const event = new Date('14 Jun 2017 00:00:00 PDT'); 2 | 3 | console.log(event.toUTCString()); 4 | // expected output: Wed, 14 Jun 2017 07:00:00 GMT -------------------------------------------------------------------------------- /JavaScript/statement/statement-for.js: -------------------------------------------------------------------------------- 1 | var str = ""; 2 | 3 | for (var i = 0; i < 9; i++) { 4 | str = str + i; 5 | } 6 | 7 | console.log(str); 8 | // expected output: "012345678" -------------------------------------------------------------------------------- /JavaScript/statement/statement-function.js: -------------------------------------------------------------------------------- 1 | function calcRectArea(width, height) { 2 | return width * height; 3 | } 4 | 5 | console.log(calcRectArea(5, 6)); 6 | // expected output: 30 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-from.js: -------------------------------------------------------------------------------- 1 | let uint16 = new Int16Array; 2 | uint16 = Int16Array.from('12345'); 3 | 4 | console.log(uint16); 5 | // expected output: Int16Array [1, 2, 3, 4, 5] -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-name.js: -------------------------------------------------------------------------------- 1 | console.log(Uint8Array.name); 2 | // expected output: "Uint8Array" 3 | 4 | console.log(Float32Array.name); 5 | // expected output: "Float32Array" -------------------------------------------------------------------------------- /JavaScript/string/string-substr.js: -------------------------------------------------------------------------------- 1 | var str = 'Mozilla'; 2 | 3 | console.log(str.substr(1, 2)); 4 | // expected output: "oz" 5 | 6 | console.log(str.substr(2)); 7 | // expected output: "zilla" -------------------------------------------------------------------------------- /JavaScript/object/object-keys.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | a: 'somestring', 3 | b: 42, 4 | c: false 5 | }; 6 | 7 | console.log(Object.keys(object1)); 8 | // expected output: Array ["a", "b", "c"] -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-map.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([25, 36, 49]); 2 | const roots = uint8.map(Math.sqrt); 3 | 4 | console.log(roots); 5 | // expected output: Uint8Array [5, 6, 7] -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-sort.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([40, 10, 50, 20, 30]); 2 | uint8.sort(); 3 | 4 | console.log(uint8); 5 | // expected output: Uint8Array [10, 20, 30, 40, 50] -------------------------------------------------------------------------------- /JavaScript/arraybuffer/arraybuffer-constructor.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | 4 | console.log(buffer.byteLength); 5 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-functionexpression.js: -------------------------------------------------------------------------------- 1 | var getRectArea = function(width, height) { 2 | return width * height; 3 | } 4 | 5 | console.log(getRectArea(3,4)); 6 | // expected output: 12 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-this.js: -------------------------------------------------------------------------------- 1 | var test = { 2 | prop: 42, 3 | func: function() { 4 | return this.prop; 5 | }, 6 | }; 7 | 8 | console.log(test.func()); 9 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/statement/statement-break.js: -------------------------------------------------------------------------------- 1 | var i = 0; 2 | 3 | while (i < 6) { 4 | if (i === 3) { 5 | break; 6 | } 7 | i = i + 1; 8 | } 9 | 10 | console.log(i); 11 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/string/string-substring.js: -------------------------------------------------------------------------------- 1 | var str = 'Mozilla'; 2 | 3 | console.log(str.substring(1, 3)); 4 | // expected output: "oz" 5 | 6 | console.log(str.substring(2)); 7 | // expected output: "zilla" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-bytes-per-element.js: -------------------------------------------------------------------------------- 1 | console.log(Float64Array.BYTES_PER_ELEMENT); 2 | // expected output: 8 3 | 4 | console.log(Int8Array.BYTES_PER_ELEMENT); 5 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/array/array-concat.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | const array2 = ['d', 'e', 'f']; 3 | 4 | console.log(array1.concat(array2)); 5 | // expected output: Array ["a", "b", "c", "d", "e", "f"] -------------------------------------------------------------------------------- /JavaScript/array/array-from.js: -------------------------------------------------------------------------------- 1 | console.log(Array.from('foo')); 2 | // expected output: Array ["f", "o", "o"] 3 | 4 | console.log(Array.from([1, 2, 3], x => x + x)); 5 | // expected output: Array [2, 4, 6] -------------------------------------------------------------------------------- /JavaScript/array/array-map.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 4, 9, 16]; 2 | 3 | // pass a function to map 4 | const map1 = array1.map(x => x * 2); 5 | 6 | console.log(map1); 7 | // expected output: Array [2, 8, 18, 32] -------------------------------------------------------------------------------- /JavaScript/array/array-unshift.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 3]; 2 | 3 | console.log(array1.unshift(4, 5)); 4 | // expected output: 5 5 | 6 | console.log(array1); 7 | // expected output: Array [4, 5, 1, 2, 3] -------------------------------------------------------------------------------- /JavaScript/date/date-getday.js: -------------------------------------------------------------------------------- 1 | const birthday = new Date('August 19, 1975 23:15:30'); 2 | const day1 = birthday.getDay(); 3 | // Sunday - Saturday : 0 - 6 4 | 5 | console.log(day1); 6 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-size.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('a', 'alpha'); 4 | map1.set('b', 'beta'); 5 | map1.set('g', 'gamma'); 6 | 7 | console.log(map1.size); 8 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-slice.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | const array1 = uint8.slice(1, 3); 3 | 4 | console.log(array1); 5 | // expected output: Uint8Array [20, 30] -------------------------------------------------------------------------------- /JavaScript/array/array-findindex.js: -------------------------------------------------------------------------------- 1 | const array1 = [5, 12, 8, 130, 44]; 2 | 3 | const isLargeNumber = (element) => element > 13; 4 | 5 | console.log(array1.findIndex(isLargeNumber)); 6 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/array/array-foreach.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | 3 | array1.forEach(element => console.log(element)); 4 | 5 | // expected output: "a" 6 | // expected output: "b" 7 | // expected output: "c" -------------------------------------------------------------------------------- /JavaScript/date/date-getmilliseconds.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 69 00:20:18'); 2 | moonLanding.setMilliseconds(123); 3 | 4 | console.log(moonLanding.getMilliseconds()); 5 | // expected output: 123 -------------------------------------------------------------------------------- /JavaScript/object/object-getownpropertynames.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | a: 1, 3 | b: 2, 4 | c: 3 5 | }; 6 | 7 | console.log(Object.getOwnPropertyNames(object1)); 8 | // expected output: Array ["a", "b", "c"] -------------------------------------------------------------------------------- /JavaScript/object/object-getprototypeof.js: -------------------------------------------------------------------------------- 1 | const prototype1 = {}; 2 | const object1 = Object.create(prototype1); 3 | 4 | console.log(Object.getPrototypeOf(object1) === prototype1); 5 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/object/object-values.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | a: 'somestring', 3 | b: 42, 4 | c: false 5 | }; 6 | 7 | console.log(Object.values(object1)); 8 | // expected output: Array ["somestring", 42, false] -------------------------------------------------------------------------------- /JavaScript/statement/statement-class.js: -------------------------------------------------------------------------------- 1 | class Polygon { 2 | constructor(height, width) { 3 | this.area = height * width; 4 | } 5 | } 6 | 7 | console.log(new Polygon(4,3).area); 8 | // expected output: 12 -------------------------------------------------------------------------------- /JavaScript/statement/statement-let.js: -------------------------------------------------------------------------------- 1 | let x = 1; 2 | 3 | if (x === 1) { 4 | let x = 2; 5 | 6 | console.log(x); 7 | // expected output: 2 8 | } 9 | 10 | console.log(x); 11 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/statement/statement-var.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | 3 | if (x === 1) { 4 | var x = 2; 5 | 6 | console.log(x); 7 | // expected output: 2 8 | } 9 | 10 | console.log(x); 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/string/string-length.js: -------------------------------------------------------------------------------- 1 | var str = 'Life, the universe and everything. Answer:'; 2 | 3 | console.log(str + ' ' + str.length); 4 | // expected output: "Life, the universe and everything. Answer: 42" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-of.js: -------------------------------------------------------------------------------- 1 | let uint16 = new Int16Array; 2 | uint16 = Int16Array.of('10', '20', '30', '40' ,'50'); 3 | 4 | console.log(uint16); 5 | // expected output: Int16Array [10, 20, 30, 40, 50] -------------------------------------------------------------------------------- /JavaScript/arraybuffer/arraybuffer-isview.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | console.log(ArrayBuffer.isView(new Int32Array())); 5 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/date/date-totimestring.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | console.log(event.toTimeString()); 4 | // expected output: 23:15:30 GMT+0200 (CEST) 5 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/function/function-length.js: -------------------------------------------------------------------------------- 1 | function func1() {} 2 | 3 | function func2(a, b) {} 4 | 5 | console.log(func1.length); 6 | // expected output: 0 7 | 8 | console.log(func2.length); 9 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/string/string-tolowercase.js: -------------------------------------------------------------------------------- 1 | var sentence = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | console.log(sentence.toLowerCase()); 4 | // expected output: "the quick brown fox jumps over the lazy dog." -------------------------------------------------------------------------------- /JavaScript/string/string-tostring.js: -------------------------------------------------------------------------------- 1 | var stringObj = new String("foo"); 2 | 3 | console.log(stringObj); 4 | // expected output: String { "foo" } 5 | 6 | console.log(stringObj.toString()); 7 | // expected output: "foo" -------------------------------------------------------------------------------- /JavaScript/string/string-touppercase.js: -------------------------------------------------------------------------------- 1 | var sentence = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | console.log(sentence.toUpperCase()); 4 | // expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG." -------------------------------------------------------------------------------- /JavaScript/string/string-valueof.js: -------------------------------------------------------------------------------- 1 | var stringObj = new String("foo"); 2 | 3 | console.log(stringObj); 4 | // expected output: String { "foo" } 5 | 6 | console.log(stringObj.valueOf()); 7 | // expected output: "foo" -------------------------------------------------------------------------------- /JavaScript/date/date-getutcmilliseconds.js: -------------------------------------------------------------------------------- 1 | const exampleDate = new Date('2018-01-02T03:04:05.678Z'); // 2 January 2018, 03:04:05.678 (UTC) 2 | 3 | console.log(exampleDate.getUTCMilliseconds()); 4 | // expected output: 678 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-commaoperators.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | 3 | x = (x++, x); 4 | 5 | console.log(x); 6 | // expected output: 2 7 | 8 | x = (2, 3); 9 | 10 | console.log(x); 11 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/functions/functions-default.js: -------------------------------------------------------------------------------- 1 | function multiply(a, b = 1) { 2 | return a * b; 3 | } 4 | 5 | console.log(multiply(5, 2)); 6 | // expected output: 10 7 | 8 | console.log(multiply(5)); 9 | // expected output: 5 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-globalthis.js: -------------------------------------------------------------------------------- 1 | function canMakeHTTPRequest() { 2 | return typeof globalThis.XMLHttpRequest === 'function'; 3 | } 4 | 5 | console.log(canMakeHTTPRequest()); 6 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/json/json-parse.js: -------------------------------------------------------------------------------- 1 | var json = '{"result":true, "count":42}'; 2 | obj = JSON.parse(json); 3 | 4 | console.log(obj.count); 5 | // expected output: 42 6 | 7 | console.log(obj.result); 8 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-has.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | map1.set('bar', 'foo'); 3 | 4 | console.log(map1.has('bar')); 5 | // expected output: true 6 | 7 | console.log(map1.has('baz')); 8 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-flags.js: -------------------------------------------------------------------------------- 1 | // outputs RegExp flags in alphabetical order 2 | 3 | console.log(/foo/ig.flags); 4 | // expected output: "gi" 5 | 6 | console.log(/bar/myu.flags); 7 | // expected output: "muy" -------------------------------------------------------------------------------- /JavaScript/sharedarraybuffer/sharedarraybuffer-bytelength.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(8); 3 | 4 | console.log(buffer.byteLength); 5 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/sharedarraybuffer/sharedarraybuffer-constructor.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(8); 3 | 4 | console.log(buffer.byteLength); 5 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/statement/statement-dowhile.js: -------------------------------------------------------------------------------- 1 | var result = ""; 2 | var i = 0; 3 | 4 | do { 5 | i = i + 1; 6 | result = result + i; 7 | } while (i < 5); 8 | 9 | console.log(result); 10 | // expected result: "12345" -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-prototype.js: -------------------------------------------------------------------------------- 1 | Symbol.prototype.toString = function() { 2 | return ('SYMBOL'); 3 | } 4 | 5 | var symbol1 = Symbol('foo'); 6 | 7 | console.log(symbol1.toString()); 8 | // expected output: "SYMBOL" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-tostring.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | 3 | const uint8String = uint8.toString(); 4 | 5 | console.log(uint8String.startsWith('10')); 6 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/array/array-every.js: -------------------------------------------------------------------------------- 1 | const isBelowThreshold = (currentValue) => currentValue < 40; 2 | 3 | const array1 = [1, 30, 39, 29, 10, 13]; 4 | 5 | console.log(array1.every(isBelowThreshold)); 6 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/array/array-some.js: -------------------------------------------------------------------------------- 1 | const array = [1, 2, 3, 4, 5]; 2 | 3 | // checks whether an element is even 4 | const even = (element) => element % 2 === 0; 5 | 6 | console.log(array.some(even)); 7 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/date/date-tostring.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | console.log(event.toString()); 4 | // expected output: Tue Aug 19 1975 23:15:30 GMT+0200 (CEST) 5 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-get.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | map1.set('bar', 'foo'); 3 | 4 | console.log(map1.get('bar')); 5 | // expected output: "foo" 6 | 7 | console.log(map1.get('baz')); 8 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-set.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | map1.set('bar', 'foo'); 3 | 4 | console.log(map1.get('bar')); 5 | // expected output: "foo" 6 | 7 | console.log(map1.get('baz')); 8 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/number/number-epsilon.js: -------------------------------------------------------------------------------- 1 | var result = Math.abs(0.2 - 0.3 + 0.1); 2 | 3 | console.log(result); 4 | // expected output: 2.7755575615628914e-17 5 | 6 | console.log(result < Number.EPSILON); 7 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/promise/promise-catch.js: -------------------------------------------------------------------------------- 1 | var promise1 = new Promise(function(resolve, reject) { 2 | throw 'Uh-oh!'; 3 | }); 4 | 5 | promise1.catch(function(error) { 6 | console.error(error); 7 | }); 8 | // expected output: Uh-oh! -------------------------------------------------------------------------------- /JavaScript/string/string-startswith.js: -------------------------------------------------------------------------------- 1 | const str1 = 'Saturday night plans'; 2 | 3 | console.log(str1.startsWith('Sat')); 4 | // expected output: true 5 | 6 | console.log(str1.startsWith('Sat', 3)); 7 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/string/string-trim.js: -------------------------------------------------------------------------------- 1 | var greeting = ' Hello world! '; 2 | 3 | console.log(greeting); 4 | // expected output: " Hello world! "; 5 | 6 | console.log(greeting.trim()); 7 | // expected output: "Hello world!"; -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-hasinstance.js: -------------------------------------------------------------------------------- 1 | class Array1 { 2 | static [Symbol.hasInstance](instance) { 3 | return Array.isArray(instance); 4 | } 5 | } 6 | 7 | console.log([] instanceof Array1); 8 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-keys.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | const keys = uint8.keys(); 3 | 4 | keys.next(); 5 | keys.next(); 6 | 7 | console.log(keys.next().value); 8 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/array/array-shift.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 3]; 2 | 3 | const firstElement = array1.shift(); 4 | 5 | console.log(array1); 6 | // expected output: Array [2, 3] 7 | 8 | console.log(firstElement); 9 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/boolean/boolean-valueof.js: -------------------------------------------------------------------------------- 1 | const x = new Boolean(); 2 | 3 | console.log(x.valueOf()); 4 | // expected output: false 5 | 6 | const y = new Boolean("Mozilla"); 7 | 8 | console.log(y.valueOf()); 9 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/date/date-gettime.js: -------------------------------------------------------------------------------- 1 | const moonLanding = new Date('July 20, 69 00:20:18 GMT+00:00'); 2 | 3 | // milliseconds since Jan 1, 1970, 00:00:00.000 GMT 4 | console.log(moonLanding.getTime()); 5 | // expected output: -14254782000 -------------------------------------------------------------------------------- /JavaScript/object/object-freeze.js: -------------------------------------------------------------------------------- 1 | const obj = { 2 | prop: 42 3 | }; 4 | 5 | Object.freeze(obj); 6 | 7 | obj.prop = 33; 8 | // Throws an error in strict mode 9 | 10 | console.log(obj.prop); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-fill.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([0, 0, 0, 0]); 2 | // (value, start position, end position); 3 | uint8.fill(4, 1, 3); 4 | 5 | console.log(uint8); 6 | // expected output: Uint8Array [0, 4, 4, 0] -------------------------------------------------------------------------------- /JavaScript/promise/promise-then.js: -------------------------------------------------------------------------------- 1 | var promise1 = new Promise(function(resolve, reject) { 2 | resolve('Success!'); 3 | }); 4 | 5 | promise1.then(function(value) { 6 | console.log(value); 7 | // expected output: "Success!" 8 | }); -------------------------------------------------------------------------------- /JavaScript/statement/statement-forof.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | 3 | for (const element of array1) { 4 | console.log(element); 5 | } 6 | 7 | // expected output: "a" 8 | // expected output: "b" 9 | // expected output: "c" -------------------------------------------------------------------------------- /JavaScript/string/string-trimend.js: -------------------------------------------------------------------------------- 1 | var greeting = ' Hello world! '; 2 | 3 | console.log(greeting); 4 | // expected output: " Hello world! "; 5 | 6 | console.log(greeting.trimEnd()); 7 | // expected output: " Hello world!"; -------------------------------------------------------------------------------- /JavaScript/string/string-trimstart.js: -------------------------------------------------------------------------------- 1 | var greeting = ' Hello world! '; 2 | 3 | console.log(greeting); 4 | // expected output: " Hello world! "; 5 | 6 | console.log(greeting.trimStart()); 7 | // expected output: "Hello world! "; -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-length.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | const uint8 = new Uint8Array(buffer, 2); 4 | 5 | console.log (uint8.length); 6 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/classes/classes-constructor.js: -------------------------------------------------------------------------------- 1 | class Polygon { 2 | constructor() { 3 | this.name = "Polygon"; 4 | } 5 | } 6 | 7 | const poly1 = new Polygon(); 8 | 9 | console.log(poly1.name); 10 | // expected output: "Polygon" -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-buffer.js: -------------------------------------------------------------------------------- 1 | //create an ArrayBuffer 2 | const buffer = new ArrayBuffer(123); 3 | 4 | // Create a view 5 | const view = new DataView(buffer); 6 | 7 | console.log(view.buffer.byteLength); 8 | // expected output: 123 -------------------------------------------------------------------------------- /JavaScript/functions/functions-arrow.js: -------------------------------------------------------------------------------- 1 | var materials = [ 2 | 'Hydrogen', 3 | 'Helium', 4 | 'Lithium', 5 | 'Beryllium' 6 | ]; 7 | 8 | console.log(materials.map(material => material.length)); 9 | // expected output: Array [8, 6, 7, 9] -------------------------------------------------------------------------------- /JavaScript/statement/statement-block.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | let y = 1; 3 | 4 | if (true) { 5 | var x = 2; 6 | let y = 2; 7 | } 8 | 9 | console.log(x); 10 | // expected output: 2 11 | 12 | console.log(y); 13 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/string/string-match.js: -------------------------------------------------------------------------------- 1 | var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; 2 | var regex = /[A-Z]/g; 3 | var found = paragraph.match(regex); 4 | 5 | console.log(found); 6 | // expected output: Array ["T", "I"] -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-bytelength.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | const uint8 = new Uint8Array(buffer, 2); 4 | 5 | console.log (uint8.byteLength); 6 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-entries.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | eArr = uint8.entries(); 3 | 4 | eArr.next(); 5 | eArr.next(); 6 | 7 | console.log(eArr.next().value); 8 | // expected output: Array [2, 30] -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-values.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([ 10, 20, 30, 40, 50]); 2 | const array1 = uint8.values(); 3 | 4 | array1.next(); 5 | array1.next(); 6 | 7 | console.log(array1.next().value); 8 | // expected output: 30 -------------------------------------------------------------------------------- /JavaScript/array/array-lastindexof.js: -------------------------------------------------------------------------------- 1 | const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo']; 2 | 3 | console.log(animals.lastIndexOf('Dodo')); 4 | // expected output: 3 5 | 6 | console.log(animals.lastIndexOf('Tiger')); 7 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/array/array-reduce-right.js: -------------------------------------------------------------------------------- 1 | const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight( 2 | (accumulator, currentValue) => accumulator.concat(currentValue) 3 | ); 4 | 5 | console.log(array1); 6 | // expected output: Array [4, 5, 2, 3, 0, 1] -------------------------------------------------------------------------------- /JavaScript/bigint/bigint-tostring.js: -------------------------------------------------------------------------------- 1 | console.log(1024n.toString()); 2 | // expected output: "1024" 3 | 4 | console.log(1024n.toString(2)); 5 | // expected output: "10000000000" 6 | 7 | console.log(1024n.toString(16)); 8 | // expected output: "400" -------------------------------------------------------------------------------- /JavaScript/number/number-isfinite.js: -------------------------------------------------------------------------------- 1 | console.log(Number.isFinite(1/0)); 2 | // expected output: false 3 | 4 | console.log(Number.isFinite(10/5)); 5 | // expected output: true 6 | 7 | console.log(Number.isFinite(0/0)); 8 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/object/object-fromentries.js: -------------------------------------------------------------------------------- 1 | const entries = new Map([ 2 | ['foo', 'bar'], 3 | ['baz', 42] 4 | ]); 5 | 6 | const obj = Object.fromEntries(entries); 7 | 8 | console.log(obj); 9 | // expected output: Object { foo: "bar", baz: 42 } -------------------------------------------------------------------------------- /JavaScript/statement/statement-continue.js: -------------------------------------------------------------------------------- 1 | var text = ""; 2 | 3 | for (var i = 0; i < 10; i++) { 4 | if (i === 3) { 5 | continue; 6 | } 7 | text = text + i; 8 | } 9 | 10 | console.log(text); 11 | // expected output: "012456789" -------------------------------------------------------------------------------- /JavaScript/string/string-concat.js: -------------------------------------------------------------------------------- 1 | var str1 = 'Hello'; 2 | var str2 = 'World'; 3 | 4 | console.log(str1.concat(' ', str2)); 5 | // expected output: "Hello World" 6 | 7 | console.log(str2.concat(', ', str1)); 8 | // expected output: "World, Hello" -------------------------------------------------------------------------------- /JavaScript/string/string-tolocaleuppercase.js: -------------------------------------------------------------------------------- 1 | var city = 'istanbul'; 2 | 3 | console.log(city.toLocaleUpperCase('en-US')); 4 | // expected output: "ISTANBUL" 5 | 6 | console.log(city.toLocaleUpperCase('TR')); 7 | // expected output: "İSTANBUL" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-buffer.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | const uint16 = new Uint16Array(buffer); 4 | 5 | console.log(uint16.buffer.byteLength); 6 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/boolean/boolean-tostring.js: -------------------------------------------------------------------------------- 1 | const flag1 = new Boolean(true); 2 | 3 | console.log(flag1.toString()); 4 | // expected output: "true" 5 | 6 | const flag2 = new Boolean(1); 7 | 8 | console.log(flag2.toString()); 9 | // expected output: "true" -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-add.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | 3 | set1.add(42); 4 | set1.add(42); 5 | set1.add(13); 6 | 7 | for (let item of set1) { 8 | console.log(item); 9 | // expected output: 42 10 | // expected output: 13 11 | } -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-clear.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | set1.add(1); 3 | set1.add('foo'); 4 | 5 | console.log(set1.size); 6 | // expected output: 2 7 | 8 | set1.clear(); 9 | 10 | console.log(set1.size); 11 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/statement/statement-empty.js: -------------------------------------------------------------------------------- 1 | var array1 = [1, 2, 3]; 2 | 3 | // Assign all array values to 0 4 | for (i = 0; i < array1.length; array1[i++] = 0) /* empty statement */ ; 5 | 6 | console.log(array1); 7 | // expected output: Array [0, 0, 0] -------------------------------------------------------------------------------- /JavaScript/statement/statement-ifelse.js: -------------------------------------------------------------------------------- 1 | function testNum(a) { 2 | if (a > 0) { 3 | return "positive"; 4 | } else { 5 | return "NOT positive"; 6 | } 7 | } 8 | 9 | console.log(testNum(-5)); 10 | // expected output: "NOT positive" -------------------------------------------------------------------------------- /JavaScript/statement/statement-label.js: -------------------------------------------------------------------------------- 1 | var str = ""; 2 | 3 | loop1: 4 | for (var i = 0; i < 5; i++) { 5 | if (i === 1) { 6 | continue loop1; 7 | } 8 | str = str + i; 9 | } 10 | 11 | console.log(str); 12 | // expected output: "0234" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-lastindexof.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 50, 50, 50, 60]); 2 | 3 | console.log(uint8.lastIndexOf(50, 5)); 4 | // expected output: 4 5 | 6 | console.log(uint8.lastIndexOf(50, 3)); 7 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/statement/statement-forin.js: -------------------------------------------------------------------------------- 1 | const object = {a: 1, b: 2, c: 3}; 2 | 3 | for (const property in object) { 4 | console.log(`${property}: ${object[property]}`); 5 | } 6 | 7 | // expected output: 8 | // "a: 1" 9 | // "b: 2" 10 | // "c: 3" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-every.js: -------------------------------------------------------------------------------- 1 | function isNegative(element, index, array) { 2 | return element < 0; 3 | } 4 | 5 | const int8 = new Int8Array([-10, -20, -30, -40, -50]); 6 | 7 | console.log(int8.every(isNegative)); 8 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-find.js: -------------------------------------------------------------------------------- 1 | function isNegative(element, index, array) { 2 | return element < 0; 3 | } 4 | 5 | const int8 = new Int8Array([10, 0, -10, 20, -30, 40, -50]); 6 | 7 | console.log(int8.find(isNegative)); 8 | // expected output: -10 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-findindex.js: -------------------------------------------------------------------------------- 1 | function isNegative(element, index, array) { 2 | return element < 0; 3 | } 4 | 5 | const int8 = new Int8Array([10, -20, 30, -40, 50]); 6 | 7 | console.log(int8.findIndex(isNegative)); 8 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-reduce.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([0, 1, 2, 3]); 2 | 3 | function sum(previousValue, currentValue) { 4 | return previousValue + currentValue; 5 | } 6 | 7 | console.log(uint8.reduce(sum)); 8 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/array/array-keys.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | const iterator = array1.keys(); 3 | 4 | for (const key of iterator) { 5 | console.log(key); 6 | } 7 | 8 | // expected output: 0 9 | // expected output: 1 10 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/date/date-setutcdate.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30 GMT-3:00'); 2 | 3 | console.log(event.getUTCDate()); 4 | // expected output: 20 5 | 6 | event.setUTCDate(19); 7 | 8 | console.log(event.getUTCDate()); 9 | // expected output: 19 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-null.js: -------------------------------------------------------------------------------- 1 | function getVowels(str) { 2 | const m = str.match(/[aeiou]/gi); 3 | if (m === null) { 4 | return 0; 5 | } 6 | return m.length; 7 | } 8 | 9 | console.log(getVowels('sky')); 10 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-undefined.js: -------------------------------------------------------------------------------- 1 | function test(t) { 2 | if (t === undefined) { 3 | return 'Undefined value!'; 4 | } 5 | return t; 6 | } 7 | 8 | let x; 9 | 10 | console.log(test(x)); 11 | // expected output: "Undefined value!" -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-global.js: -------------------------------------------------------------------------------- 1 | var regex1 = new RegExp('foo', 'g'); 2 | 3 | console.log(regex1.global); 4 | // expected output: true 5 | 6 | var regex2 = new RegExp('bar', 'i'); 7 | 8 | console.log(regex2.global); 9 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-size.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | var object1 = new Object(); 3 | 4 | set1.add(42); 5 | set1.add('forty two'); 6 | set1.add('forty two'); 7 | set1.add(object1); 8 | 9 | console.log(set1.size); 10 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/string/string-padend.js: -------------------------------------------------------------------------------- 1 | const str1 = 'Breaded Mushrooms'; 2 | 3 | console.log(str1.padEnd(25, '.')); 4 | // expected output: "Breaded Mushrooms........" 5 | 6 | const str2 = '200'; 7 | 8 | console.log(str2.padEnd(5)); 9 | // expected output: "200 " -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-iterator.js: -------------------------------------------------------------------------------- 1 | const iterable1 = new Object(); 2 | 3 | iterable1[Symbol.iterator] = function* () { 4 | yield 1; 5 | yield 2; 6 | yield 3; 7 | }; 8 | 9 | console.log([...iterable1]); 10 | // expected output: Array [1, 2, 3] -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-matchall.js: -------------------------------------------------------------------------------- 1 | let re = /[0-9]+/g; 2 | let str = '2016-01-02|2019-03-07'; 3 | let result = re[Symbol.matchAll](str); 4 | 5 | console.log(Array.from(result, x => x[0])); 6 | // expected output: Array ["2016", "01", "02", "2019", "03", "07"] -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-toprimitive.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | [Symbol.toPrimitive](hint) { 3 | if (hint == 'number') { 4 | return 42; 5 | } 6 | return null; 7 | } 8 | }; 9 | 10 | console.log(+object1); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-tostringtag.js: -------------------------------------------------------------------------------- 1 | class ValidatorClass { 2 | get [Symbol.toStringTag]() { 3 | return 'Validator'; 4 | } 5 | } 6 | 7 | console.log(Object.prototype.toString.call(new ValidatorClass())); 8 | // expected output: "[object Validator]" -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-yieldasterisk.js: -------------------------------------------------------------------------------- 1 | function* func1() { 2 | yield 42; 3 | } 4 | 5 | function* func2() { 6 | yield* func1(); 7 | } 8 | 9 | const iterator = func2(); 10 | 11 | console.log(iterator.next().value); 12 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-clear.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('bar', 'baz'); 4 | map1.set(1, 'foo'); 5 | 6 | console.log(map1.size); 7 | // expected output: 2 8 | 9 | map1.clear(); 10 | 11 | console.log(map1.size); 12 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/math/math-max.js: -------------------------------------------------------------------------------- 1 | console.log(Math.max(1, 3, 2)); 2 | // expected output: 3 3 | 4 | console.log(Math.max(-1, -3, -2)); 5 | // expected output: -1 6 | 7 | var array1 = [1, 3, 2]; 8 | 9 | console.log(Math.max(...array1)); 10 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/math/math-min.js: -------------------------------------------------------------------------------- 1 | console.log(Math.min(2, 3, 1)); 2 | // expected output: 1 3 | 4 | console.log(Math.min(-2, -3, -1)); 5 | // expected output: -3 6 | 7 | var array1 = [2, 3, 1]; 8 | 9 | console.log(Math.min(...array1)); 10 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/object/object-isextensible.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | console.log(Object.isExtensible(object1)); 4 | // expected output: true 5 | 6 | Object.preventExtensions(object1); 7 | 8 | console.log(Object.isExtensible(object1)); 9 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/string/string-charat.js: -------------------------------------------------------------------------------- 1 | var sentence = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | var index = 4; 4 | 5 | console.log('The character at index ' + index + ' is ' + sentence.charAt(index)); 6 | // expected output: "The character at index 4 is q" -------------------------------------------------------------------------------- /JavaScript/string/string-endswith.js: -------------------------------------------------------------------------------- 1 | const str1 = 'Cats are the best!'; 2 | 3 | console.log(str1.endsWith('best', 17)); 4 | // expected output: true 5 | 6 | const str2 = 'Is this a question'; 7 | 8 | console.log(str2.endsWith('?')); 9 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-values.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | const iterator = array1.values(); 3 | 4 | for (const value of iterator) { 5 | console.log(value); 6 | } 7 | 8 | // expected output: "a" 9 | // expected output: "b" 10 | // expected output: "c" -------------------------------------------------------------------------------- /JavaScript/arraybuffer/arraybuffer-bytelength.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | 4 | // use byteLength to check the size 5 | const bytes = buffer.byteLength; 6 | 7 | console.log(bytes); 8 | // expected output: 8 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-assignment.js: -------------------------------------------------------------------------------- 1 | var x = 2; 2 | var y = 3; 3 | 4 | console.log(x); 5 | // expected output: 2 6 | 7 | console.log(x = y + 1); // 3 + 1 8 | // expected output: 4 9 | 10 | console.log(x = x * y); // 4 * 3 11 | // expected output: 12 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-delete.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | map1.set('bar', 'foo'); 3 | 4 | console.log(map1.delete('bar')); 5 | // expected result: true 6 | // (true indicates successful removal) 7 | 8 | console.log(map1.has('bar')); 9 | // expected result: false -------------------------------------------------------------------------------- /JavaScript/math/math-pi.js: -------------------------------------------------------------------------------- 1 | function calculateCircumference(radius) { 2 | return 2 * Math.PI * radius; 3 | } 4 | 5 | console.log(Math.PI); 6 | // expected output: 3.141592653589793 7 | 8 | console.log(calculateCircumference(10)); 9 | // expected output: 62.83185307179586 -------------------------------------------------------------------------------- /JavaScript/number/number-nan.js: -------------------------------------------------------------------------------- 1 | function clean(x) { 2 | if (x === Number.NaN) { 3 | // can never be true 4 | return null; 5 | } 6 | if (isNaN(x)) { 7 | return 0; 8 | } 9 | } 10 | 11 | console.log(clean(Number.NaN)); 12 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/string/string-tolocalelowercase.js: -------------------------------------------------------------------------------- 1 | var dotted = 'İstanbul'; 2 | 3 | console.log('EN-US: ' + dotted.toLocaleLowerCase('en-US')); 4 | // expected output: "i̇stanbul" 5 | 6 | console.log('TR: ' + dotted.toLocaleLowerCase('tr')); 7 | // expected output: "istanbul" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-includes.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | 3 | console.log(uint8.includes(20)); 4 | // expected output: true 5 | 6 | // check from position 3 7 | console.log(uint8.includes(20, 3)); 8 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-filter.js: -------------------------------------------------------------------------------- 1 | const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; 2 | 3 | const result = words.filter(word => word.length > 6); 4 | 5 | console.log(result); 6 | // expected output: Array ["exuberant", "destruction", "present"] -------------------------------------------------------------------------------- /JavaScript/classes/classes-static.js: -------------------------------------------------------------------------------- 1 | class ClassWithStaticMethod { 2 | static staticMethod() { 3 | return 'static method has been called.'; 4 | } 5 | } 6 | 7 | console.log(ClassWithStaticMethod.staticMethod()); 8 | // expected output: "static method has been called." -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-byteoffset.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer,12,4); //from byte 12 for the next 4 bytes 5 | 6 | console.log(view.byteOffset); 7 | // expected output: 12 -------------------------------------------------------------------------------- /JavaScript/date/date-setmilliseconds.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | console.log(event.getMilliseconds()); 4 | // expected output: 0 5 | 6 | event.setMilliseconds(456); 7 | 8 | console.log(event.getMilliseconds()); 9 | // expected output: 456 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-newtarget.js: -------------------------------------------------------------------------------- 1 | function Foo() { 2 | if (!new.target) throw 'Foo() must be called with new'; 3 | } 4 | 5 | try { 6 | Foo(); 7 | } 8 | catch(e) { 9 | console.log(e); 10 | // expected output: "Foo() must be called with new" 11 | } -------------------------------------------------------------------------------- /JavaScript/function/function-name.js: -------------------------------------------------------------------------------- 1 | const func1 = function() {} 2 | 3 | const object = { 4 | func2: function() {} 5 | } 6 | 7 | console.log(func1.name); 8 | // expected output: "func1" 9 | 10 | console.log(object.func2.name); 11 | // expected output: "func2" -------------------------------------------------------------------------------- /JavaScript/object/object-issealed.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | console.log(Object.isSealed(object1)); 6 | // expected output: false 7 | 8 | Object.seal(object1); 9 | 10 | console.log(Object.isSealed(object1)); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-preventextensions.js: -------------------------------------------------------------------------------- 1 | var object1 = {}; 2 | 3 | console.log(Reflect.isExtensible(object1)); 4 | // expected output: true 5 | 6 | Reflect.preventExtensions(object1); 7 | 8 | console.log(Reflect.isExtensible(object1)); 9 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-has.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set([1, 2, 3, 4, 5]); 2 | 3 | console.log(set1.has(1)); 4 | // expected output: true 5 | 6 | console.log(set1.has(5)); 7 | // expected output: true 8 | 9 | console.log(set1.has(6)); 10 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/statement/statement-trycatch.js: -------------------------------------------------------------------------------- 1 | try { 2 | nonExistentFunction(); 3 | } 4 | catch(error) { 5 | console.error(error); 6 | // expected output: ReferenceError: nonExistentFunction is not defined 7 | // Note - error messages will vary depending on browser 8 | } -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-subarray.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | 3 | console.log(uint8.subarray(1, 3)); 4 | // expected output: Uint8Array [20, 30] 5 | 6 | console.log(uint8.subarray(1)); 7 | // expected output: Uint8Array [20, 30, 40, 50] -------------------------------------------------------------------------------- /JavaScript/array/array-entries.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c']; 2 | 3 | const iterator1 = array1.entries(); 4 | 5 | console.log(iterator1.next().value); 6 | // expected output: Array [0, "a"] 7 | 8 | console.log(iterator1.next().value); 9 | // expected output: Array [1, "b"] -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-islockfree.js: -------------------------------------------------------------------------------- 1 | console.log(Atomics.isLockFree(3)); 2 | // 3 is not one of the BYTES_PER_ELEMENT values 3 | // expected output: false 4 | 5 | console.log(Atomics.isLockFree(4)); 6 | // 4 is one of the BYTES_PER_ELEMENT values 7 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/date/date-setutcminutes.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00'); 2 | 3 | console.log(date1.getUTCMinutes()); 4 | // expected output: 15 5 | 6 | date1.setUTCMinutes(25); 7 | 8 | console.log(date1.getUTCMinutes()); 9 | // expected output: 25 -------------------------------------------------------------------------------- /JavaScript/date/date-setutcseconds.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00'); 2 | 3 | console.log(date1.getUTCSeconds()); 4 | // expected output: 30 5 | 6 | date1.setUTCSeconds(39); 7 | 8 | console.log(date1.getUTCSeconds()); 9 | // expected output: 39 -------------------------------------------------------------------------------- /JavaScript/math/math-sign.js: -------------------------------------------------------------------------------- 1 | console.log(Math.sign(3)); 2 | // expected output: 1 3 | 4 | console.log(Math.sign(-3)); 5 | // expected output: -1 6 | 7 | console.log(Math.sign(0)); 8 | // expected output: 0 9 | 10 | console.log(Math.sign('-3')); 11 | // expected output: -1 -------------------------------------------------------------------------------- /JavaScript/object/object-isfrozen.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | console.log(Object.isFrozen(object1)); 6 | // expected output: false 7 | 8 | Object.freeze(object1); 9 | 10 | console.log(Object.isFrozen(object1)); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-get.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | x: 1, 3 | y: 2 4 | }; 5 | 6 | console.log(Reflect.get(object1, 'x')); 7 | // expected output: 1 8 | 9 | var array1 = ['zero', 'one']; 10 | 11 | console.log(Reflect.get(array1, 1)); 12 | // expected output: "one" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-copywithin.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8 ]); 2 | 3 | // (insert position, start position, end position) 4 | uint8.copyWithin(3, 1, 3); 5 | 6 | console.log(uint8); 7 | // expected output: Uint8Array [1, 2, 3, 2, 3, 6, 7, 8] -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getfloat64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setFloat64(1, Math.PI); 6 | 7 | console.log(view.getFloat64(1)); 8 | // expected output: 3.141592653589793 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setfloat64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setFloat64(1, Math.PI); 6 | 7 | console.log(view.getFloat64(1)); 8 | // expected output: 3.141592653589793 -------------------------------------------------------------------------------- /JavaScript/date/date-setutcmilliseconds.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('2018-01-24T12:38:29.069Z'); 2 | 3 | console.log(date1.getUTCMilliseconds()); 4 | // expected output: 69 5 | 6 | date1.setUTCMilliseconds(420); 7 | 8 | console.log(date1.getUTCMilliseconds()); 9 | // expected output: 420 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-operatorprecedence.js: -------------------------------------------------------------------------------- 1 | console.log(3 + 4 * 5); // 3 + 20 2 | // expected output: 23 3 | 4 | console.log(4 * 3 ** 2); // 4 * 9 5 | // expected output: 36 6 | 7 | var a; 8 | var b; 9 | 10 | console.log(a = b = 5); 11 | // expected output: 5; -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-nan.js: -------------------------------------------------------------------------------- 1 | function sanitise(x) { 2 | if (isNaN(x)) { 3 | return NaN; 4 | } 5 | return x; 6 | } 7 | 8 | console.log(sanitise('1')); 9 | // expected output: "1" 10 | 11 | console.log(sanitise('NotANumber')); 12 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/object/object-defineproperties.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | Object.defineProperties(object1, { 4 | property1: { 5 | value: 42, 6 | writable: true 7 | }, 8 | property2: {} 9 | }); 10 | 11 | console.log(object1.property1); 12 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/promise/promise-reject.js: -------------------------------------------------------------------------------- 1 | function resolved(result) { 2 | console.log('Resolved'); 3 | } 4 | 5 | function rejected(result) { 6 | console.error(result); 7 | } 8 | 9 | Promise.reject(new Error('fail')).then(resolved, rejected); 10 | // expected output: Error: fail -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-constructor.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set([1, 2, 3, 4, 5]); 2 | 3 | console.log(set1.has(1)); 4 | // expected output: true 5 | 6 | console.log(set1.has(5)); 7 | // expected output: true 8 | 9 | console.log(set1.has(6)); 10 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-unscopables.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | object1[Symbol.unscopables] = { 6 | property1: true 7 | }; 8 | 9 | with (object1) { 10 | console.log(property1); 11 | // expected output: Error: property1 is not defined 12 | } -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getfloat32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setFloat32(1, Math.PI); 6 | 7 | console.log(view.getFloat32(1)); 8 | // expected output: 3.1415927410125732 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getint8.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt8(1, 127); // (max signed 8-bit integer) 6 | 7 | console.log(view.getInt8(1)); 8 | // expected output: 127 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setfloat32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setFloat32(1, Math.PI); 6 | 7 | console.log(view.getFloat32(1)); 8 | // expected output: 3.1415927410125732 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setint8.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt8(1, 127); // (max signed 8-bit integer) 6 | 7 | console.log(view.getInt8(1)); 8 | // expected output: 127 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-functionasteriskexpression.js: -------------------------------------------------------------------------------- 1 | function* foo() { 2 | yield 'a'; 3 | yield 'b'; 4 | yield 'c'; 5 | } 6 | 7 | var str = ""; 8 | for (let val of foo()) { 9 | str = str + val; 10 | } 11 | 12 | console.log(str); 13 | // expected output: "abc" -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-spreadsyntax.js: -------------------------------------------------------------------------------- 1 | function sum(x, y, z) { 2 | return x + y + z; 3 | } 4 | 5 | const numbers = [1, 2, 3]; 6 | 7 | console.log(sum(...numbers)); 8 | // expected output: 6 9 | 10 | console.log(sum.apply(null, numbers)); 11 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-voidoperator.js: -------------------------------------------------------------------------------- 1 | void function test() { 2 | console.log('boo!'); 3 | // expected output: "boo!" 4 | }(); 5 | 6 | try { 7 | test(); 8 | } 9 | catch(e) { 10 | console.log(e); 11 | // expected output: ReferenceError: test is not defined 12 | } -------------------------------------------------------------------------------- /JavaScript/function/function-apply.js: -------------------------------------------------------------------------------- 1 | const numbers = [5, 6, 2, 3, 7]; 2 | 3 | const max = Math.max.apply(null, numbers); 4 | 5 | console.log(max); 6 | // expected output: 7 7 | 8 | const min = Math.min.apply(null, numbers); 9 | 10 | console.log(min); 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/json/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "json-parse.js", 4 | "title": "JavaScript: JSON.parse()", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "json-stringify.js", 9 | "title": "JavaScript: JSON.stringify()", 10 | "type": "js" 11 | } 12 | ] -------------------------------------------------------------------------------- /JavaScript/math/math-ceil.js: -------------------------------------------------------------------------------- 1 | console.log(Math.ceil(.95)); 2 | // expected output: 1 3 | 4 | console.log(Math.ceil(4)); 5 | // expected output: 4 6 | 7 | console.log(Math.ceil(7.004)); 8 | // expected output: 8 9 | 10 | console.log(Math.ceil(-7.004)); 11 | // expected output: -7 -------------------------------------------------------------------------------- /JavaScript/math/math-floor.js: -------------------------------------------------------------------------------- 1 | console.log(Math.floor(5.95)); 2 | // expected output: 5 3 | 4 | console.log(Math.floor(5.05)); 5 | // expected output: 5 6 | 7 | console.log(Math.floor(5)); 8 | // expected output: 5 9 | 10 | console.log(Math.floor(-5.05)); 11 | // expected output: -6 -------------------------------------------------------------------------------- /JavaScript/math/math-log.js: -------------------------------------------------------------------------------- 1 | function getBaseLog(x, y) { 2 | return Math.log(y) / Math.log(x); 3 | } 4 | 5 | // 2 x 2 x 2 = 8 6 | console.log(getBaseLog(2, 8)); 7 | // expected output: 3 8 | 9 | // 5 x 5 x 5 x 5 = 625 10 | console.log(getBaseLog(5, 625)); 11 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/number/number-valueof.js: -------------------------------------------------------------------------------- 1 | var numObj = new Number(42); 2 | console.log(typeof numObj); 3 | // expected output: "object" 4 | 5 | var num = numObj.valueOf(); 6 | console.log(num); 7 | // expected output: 42 8 | 9 | console.log(typeof num); 10 | // expected output: "number" -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-@@replace.js: -------------------------------------------------------------------------------- 1 | class RegExp1 extends RegExp { 2 | [Symbol.replace](str) { 3 | return RegExp.prototype[Symbol.replace].call(this, str, '#!@?'); 4 | } 5 | } 6 | 7 | console.log('football'.replace(new RegExp1('foo'))); 8 | // expected output: "#!@?tball" -------------------------------------------------------------------------------- /JavaScript/string/string-includes.js: -------------------------------------------------------------------------------- 1 | var sentence = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | var word = 'fox'; 4 | 5 | console.log(`The word "${word}" ${sentence.includes(word)? 'is' : 'is not'} in the sentence`); 6 | // expected output: "The word "fox" is in the sentence" -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getuint8.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint8(1, 255); // (max unsigned 8-bit integer) 6 | 7 | console.log(view.getUint8(1)); 8 | // expected output: 255 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setuint8.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint8(1, 255); // (max unsigned 8-bit integer) 6 | 7 | console.log(view.getUint8(1)); 8 | // expected output: 255 -------------------------------------------------------------------------------- /JavaScript/date/date-valueof.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5)); 2 | 3 | console.log(date1.valueOf()); 4 | // expected output: 823230245000 5 | 6 | const date2 = new Date('02 Feb 1996 03:04:05 GMT'); 7 | 8 | console.log(date2.valueOf()); 9 | // expected output: 823230245000 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-logicaloperator.js: -------------------------------------------------------------------------------- 1 | var a = 3; 2 | var b = -2; 3 | 4 | console.log(a > 0 && b > 0); 5 | // expected output: false 6 | 7 | console.log(a > 0 || b > 0); 8 | // expected output: true 9 | 10 | console.log(!(a > 0 || b > 0)); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-newoperator.js: -------------------------------------------------------------------------------- 1 | function Car(make, model, year) { 2 | this.make = make; 3 | this.model = model; 4 | this.year = year; 5 | } 6 | 7 | var car1 = new Car('Eagle', 'Talon TSi', 1993); 8 | 9 | console.log(car1.make); 10 | // expected output: "Eagle" -------------------------------------------------------------------------------- /JavaScript/functions/functions-setter.js: -------------------------------------------------------------------------------- 1 | var language = { 2 | set current(name) { 3 | this.log.push(name); 4 | }, 5 | log: [] 6 | } 7 | 8 | language.current = 'EN'; 9 | language.current = 'FA'; 10 | 11 | console.log(language.log); 12 | // expected output: Array ["EN", "FA"] -------------------------------------------------------------------------------- /JavaScript/math/math-cbrt.js: -------------------------------------------------------------------------------- 1 | console.log(Math.cbrt(-1)); 2 | // expected output: -1 3 | 4 | console.log(Math.cbrt(1)); 5 | // expected output: 1 6 | 7 | console.log(Math.cbrt(Infinity)); 8 | // expected output: Infinity 9 | 10 | console.log(Math.cbrt(64)); 11 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/number/number-tostring.js: -------------------------------------------------------------------------------- 1 | function hexColour(c) { 2 | if (c < 256) { 3 | return Math.abs(c).toString(16); 4 | } 5 | return 0; 6 | } 7 | 8 | console.log(hexColour(233)); 9 | // expected output: "e9" 10 | 11 | console.log(hexColour('11')); 12 | // expected output: "b" -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-constructor.js: -------------------------------------------------------------------------------- 1 | var regex1 = /\w+/; 2 | var regex2 = new RegExp('\\w+'); 3 | 4 | console.log(regex1); 5 | // expected output: /\w+/ 6 | 7 | console.log(regex2); 8 | // expected output: /\w+/ 9 | 10 | console.log(regex1 === regex2); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/statement/statement-functionasterisk.js: -------------------------------------------------------------------------------- 1 | function* generator(i) { 2 | yield i; 3 | yield i + 10; 4 | } 5 | 6 | var gen = generator(10); 7 | 8 | console.log(gen.next().value); 9 | // expected output: 10 10 | 11 | console.log(gen.next().value); 12 | // expected output: 20 -------------------------------------------------------------------------------- /JavaScript/string/string-localecompare.js: -------------------------------------------------------------------------------- 1 | var a = 'réservé'; // with accents, lowercase 2 | var b = 'RESERVE'; // no accents, uppercase 3 | 4 | console.log(a.localeCompare(b)); 5 | // expected output: 1 6 | console.log(a.localeCompare(b, 'en', {sensitivity: 'base'})); 7 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-search.js: -------------------------------------------------------------------------------- 1 | class Search1 { 2 | constructor(value) { 3 | this.value = value; 4 | } 5 | [Symbol.search](string) { 6 | return string.indexOf(this.value); 7 | } 8 | } 9 | 10 | console.log('foobar'.search(new Search1('bar'))); 11 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getint16.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt16(1, 32767); // (max signed 16-bit integer) 6 | 7 | console.log(view.getInt16(1)); 8 | // expected output: 32767 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setint16.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt16(1, 32767); // (max signed 16-bit integer) 6 | 7 | console.log(view.getInt16(1)); 8 | // expected output: 32767 -------------------------------------------------------------------------------- /JavaScript/date/date-getutcdate.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00'); 2 | const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00'); 3 | 4 | console.log(date1.getUTCDate()); 5 | // expected output: 19 6 | 7 | console.log(date2.getUTCDate()); 8 | // expected output: 20 -------------------------------------------------------------------------------- /JavaScript/date/date-parse.js: -------------------------------------------------------------------------------- 1 | const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT'); 2 | const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT'); 3 | 4 | console.log(unixTimeZero); 5 | // expected output: 0 6 | 7 | console.log(javaScriptRelease); 8 | // expected output: 818035920000 -------------------------------------------------------------------------------- /JavaScript/date/date-setfullyear.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | event.setFullYear(1969); 4 | 5 | console.log(event.getFullYear()); 6 | // expected output: 1969 7 | 8 | event.setFullYear(0); 9 | 10 | console.log(event.getFullYear()); 11 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/date/date-setmonth.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | event.setMonth(3); 4 | 5 | console.log(event.getMonth()); 6 | // expected output: 3 7 | 8 | console.log(event); 9 | // Sat Apr 19 1975 23:15:30 GMT+0100 (CET) 10 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-comparisonoperators.js: -------------------------------------------------------------------------------- 1 | console.log(1 == 1); 2 | // expected output: true 3 | 4 | console.log("1" == 1); 5 | // expected output: true 6 | 7 | console.log(1 === 1); 8 | // expected output: true 9 | 10 | console.log("1" === 1); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-encodeuricomponent.js: -------------------------------------------------------------------------------- 1 | // encodes characters such as ?,=,/,&,: 2 | console.log(encodeURIComponent('?x=шеллы')); 3 | // expected output: "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" 4 | 5 | console.log(encodeURIComponent('?x=test')); 6 | // expected output: "%3Fx%3Dtest" -------------------------------------------------------------------------------- /JavaScript/math/math-log2.js: -------------------------------------------------------------------------------- 1 | console.log(Math.log2(3)); 2 | // expected output: 1.584962500721156 3 | 4 | console.log(Math.log2(2)); 5 | // expected output: 1 6 | 7 | console.log(Math.log2(1)); 8 | // expected output: 0 9 | 10 | console.log(Math.log2(0)); 11 | // expected output: -Infinity -------------------------------------------------------------------------------- /JavaScript/math/math-round.js: -------------------------------------------------------------------------------- 1 | console.log(Math.round(0.9)); 2 | // expected output: 1 3 | 4 | console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); 5 | // expected output: 6 6 5 6 | 7 | console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95)); 8 | // expected output: -5 -5 -6 -------------------------------------------------------------------------------- /JavaScript/math/math-trunc.js: -------------------------------------------------------------------------------- 1 | console.log(Math.trunc(13.37)); 2 | // expected output: 13 3 | 4 | console.log(Math.trunc(42.84)); 5 | // expected output: 42 6 | 7 | console.log(Math.trunc(0.123)); 8 | // expected output: 0 9 | 10 | console.log(Math.trunc(-0.123)); 11 | // expected output: -0 -------------------------------------------------------------------------------- /JavaScript/weakmap/weakmap-prototype-delete.js: -------------------------------------------------------------------------------- 1 | const weakmap1 = new WeakMap(); 2 | const object1 = {}; 3 | 4 | weakmap1.set(object1, 42); 5 | 6 | console.log(weakmap1.delete(object1)); 7 | // expected output: true 8 | 9 | console.log(weakmap1.has(object1)); 10 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getuint16.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint16(1, 65535); // (max unsigned 16-bit integer) 6 | 7 | console.log(view.getUint16(1)); 8 | // expected output: 65535 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setuint16.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint16(1, 65535); // (max unsigned 16-bit integer) 6 | 7 | console.log(view.getUint16(1)); 8 | // expected output: 65535 -------------------------------------------------------------------------------- /JavaScript/date/date-getutchours.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00'); 2 | const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00'); 3 | 4 | console.log(date1.getUTCHours()); 5 | // expected output: 12 6 | 7 | console.log(date2.getUTCHours()); 8 | // expected output: 10 -------------------------------------------------------------------------------- /JavaScript/functions/functions-arguments.js: -------------------------------------------------------------------------------- 1 | function func1(a, b, c) { 2 | console.log(arguments[0]); 3 | // expected output: 1 4 | 5 | console.log(arguments[1]); 6 | // expected output: 2 7 | 8 | console.log(arguments[2]); 9 | // expected output: 3 10 | } 11 | 12 | func1(1, 2, 3); -------------------------------------------------------------------------------- /JavaScript/functions/functions-getter.js: -------------------------------------------------------------------------------- 1 | var obj = { 2 | log: ['a', 'b', 'c'], 3 | get latest() { 4 | if (this.log.length == 0) { 5 | return undefined; 6 | } 7 | return this.log[this.log.length - 1]; 8 | } 9 | } 10 | 11 | console.log(obj.latest); 12 | // expected output: "c" -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-keys.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('0', 'foo'); 4 | map1.set(1, 'bar'); 5 | 6 | var iterator1 = map1.keys(); 7 | 8 | console.log(iterator1.next().value); 9 | // expected output: "0" 10 | 11 | console.log(iterator1.next().value); 12 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-valueof.js: -------------------------------------------------------------------------------- 1 | function MyNumberType(n) { 2 | this.number = n; 3 | } 4 | 5 | MyNumberType.prototype.valueOf = function() { 6 | return this.number; 7 | }; 8 | 9 | const object1 = new MyNumberType(4); 10 | 11 | console.log(object1 + 3); 12 | // expected output: 7 -------------------------------------------------------------------------------- /JavaScript/string/string-charcodeat.js: -------------------------------------------------------------------------------- 1 | var sentence = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | var index = 4; 4 | 5 | console.log('The character code ' + sentence.charCodeAt(index) + ' is equal to ' + sentence.charAt(index)); 6 | // expected output: "The character code 113 is equal to q" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-filter.js: -------------------------------------------------------------------------------- 1 | function isNegative(element, index, array) { 2 | return element < 0; 3 | } 4 | 5 | const int8 = new Int8Array([-10, 20, -30, 40, -50]); 6 | const negInt8 = int8.filter(isNegative); 7 | 8 | console.log(negInt8); 9 | // expected output: Int8Array [-10, -30, -50] -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getint32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt32(1, 2147483647); // (max signed 32-bit integer) 6 | 7 | console.log(view.getInt32(1)); 8 | // expected output: 2147483647 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setint32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setInt32(1, 2147483647); // (max signed 32-bit integer) 6 | 7 | console.log(view.getInt32(1)); 8 | // expected output: 2147483647 -------------------------------------------------------------------------------- /JavaScript/date/date-setdate.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | event.setDate(24); 4 | 5 | console.log(event.getDate()); 6 | // expected output: 24 7 | 8 | event.setDate(32); 9 | // Only 31 days in August! 10 | 11 | console.log(event.getDate()); 12 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/date/date-setseconds.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | event.setSeconds(42); 4 | 5 | console.log(event.getSeconds()); 6 | // expected output: 42 7 | 8 | console.log(event); 9 | // Sat Aug 19 1975 23:15:42 GMT+0100 (CET) 10 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/date/date-todatestring.js: -------------------------------------------------------------------------------- 1 | const event = new Date(1993, 6, 28, 14, 39, 7); 2 | 3 | console.log(event.toString()); 4 | // expected output: Wed Jul 28 1993 14:39:07 GMT+0200 (CEST) 5 | // (note: your timezone may vary) 6 | 7 | console.log(event.toDateString()); 8 | // expected output: Wed Jul 28 1993 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-infinity.js: -------------------------------------------------------------------------------- 1 | const maxNumber = Math.pow(10, 1000); // max positive number 2 | 3 | if (maxNumber === Infinity) { 4 | console.log("Let's call it Infinity!"); 5 | // expected output: "Let's call it Infinity!" 6 | } 7 | 8 | console.log(1 / maxNumber); 9 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/math/math-log10.js: -------------------------------------------------------------------------------- 1 | console.log(Math.log10(100000)); 2 | // expected output: 5 3 | 4 | console.log(Math.log10(2)); 5 | // expected output: 0.3010299956639812 6 | 7 | console.log(Math.log10(1)); 8 | // expected output: 0 9 | 10 | console.log(Math.log10(0)); 11 | // expected output: -Infinity -------------------------------------------------------------------------------- /JavaScript/math/math-log1p.js: -------------------------------------------------------------------------------- 1 | console.log(Math.log1p(1)); 2 | // expected output: 0.6931471805599453 3 | 4 | console.log(Math.log1p(0)); 5 | // expected output: 0 6 | 7 | console.log(Math.log1p(-1)); 8 | // expected output: -Infinity 9 | 10 | console.log(Math.log1p(-2)); 11 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/object/object-entries.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | a: 'somestring', 3 | b: 42 4 | }; 5 | 6 | for (let [key, value] of Object.entries(object1)) { 7 | console.log(`${key}: ${value}`); 8 | } 9 | 10 | // expected output: 11 | // "a: somestring" 12 | // "b: 42" 13 | // order is not guaranteed -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-tostring.js: -------------------------------------------------------------------------------- 1 | function Dog(name) { 2 | this.name = name; 3 | } 4 | 5 | var dog1 = new Dog('Gabby'); 6 | 7 | Dog.prototype.toString = function dogToString() { 8 | return '' + this.name; 9 | } 10 | 11 | console.log(dog1.toString()); 12 | // expected output: "Gabby" -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-getprototypeof.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | const proto1 = Reflect.getPrototypeOf(object1); 6 | 7 | console.log(proto1); 8 | // expected output: [object Object] 9 | 10 | console.log(Reflect.getPrototypeOf(proto1)); 11 | // expected output: null -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-values.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | set1.add(42); 3 | set1.add('forty two'); 4 | 5 | var iterator1 = set1.values(); 6 | 7 | console.log(iterator1.next().value); 8 | // expected output: 42 9 | 10 | console.log(iterator1.next().value); 11 | // expected output: "forty two" -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getuint32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint32(1, 4294967295); // (max unsigned 32-bit integer) 6 | 7 | console.log(view.getUint32(1)); 8 | // expected output: 4294967295 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setuint32.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view = new DataView(buffer); 5 | view.setUint32(1, 4294967295); // (max unsigned 32-bit integer) 6 | 7 | console.log(view.getUint32(1)); 8 | // expected output: 4294967295 -------------------------------------------------------------------------------- /JavaScript/date/date-toisostring.js: -------------------------------------------------------------------------------- 1 | const event = new Date('05 October 2011 14:48 UTC'); 2 | console.log(event.toString()); 3 | // expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST) 4 | // (note: your timezone may vary) 5 | 6 | console.log(event.toISOString()); 7 | // expected output: 2011-10-05T14:48:00.000Z -------------------------------------------------------------------------------- /JavaScript/date/date-tojson.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30 UTC'); 2 | 3 | const jsonDate = event.toJSON(); 4 | 5 | console.log(jsonDate); 6 | // expected output: 1975-08-19T23:15:30.000Z 7 | 8 | console.log(new Date(jsonDate).toUTCString()); 9 | // expected output: Tue, 19 Aug 1975 23:15:30 GMT -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-deleteoperator.js: -------------------------------------------------------------------------------- 1 | var Employee = { 2 | firstname: "John", 3 | lastname: "Doe" 4 | } 5 | 6 | console.log(Employee.firstname); 7 | // expected output: "John" 8 | 9 | delete Employee.firstname; 10 | 11 | console.log(Employee.firstname); 12 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-yield.js: -------------------------------------------------------------------------------- 1 | function* foo(index) { 2 | while (index < 2) { 3 | yield index++; 4 | } 5 | } 6 | 7 | const iterator = foo(0); 8 | 9 | console.log(iterator.next().value); 10 | // expected output: 0 11 | 12 | console.log(iterator.next().value); 13 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/functions/functions-restparameters.js: -------------------------------------------------------------------------------- 1 | function sum(...theArgs) { 2 | return theArgs.reduce((previous, current) => { 3 | return previous + current; 4 | }); 5 | } 6 | 7 | console.log(sum(1, 2, 3)); 8 | // expected output: 6 9 | 10 | console.log(sum(1, 2, 3, 4)); 11 | // expected output: 10 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-values.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('0', 'foo'); 4 | map1.set(1, 'bar'); 5 | 6 | var iterator1 = map1.values(); 7 | 8 | console.log(iterator1.next().value); 9 | // expected output: "foo" 10 | 11 | console.log(iterator1.next().value); 12 | // expected output: "bar" -------------------------------------------------------------------------------- /JavaScript/math/math-hypot.js: -------------------------------------------------------------------------------- 1 | console.log(Math.hypot(3, 4)); 2 | // expected output: 5 3 | 4 | console.log(Math.hypot(5, 12)); 5 | // expected output: 13 6 | 7 | console.log(Math.hypot(3, 4, 5)); 8 | // expected output: 7.0710678118654755 9 | 10 | console.log(Math.hypot(-5)); 11 | // expected output: 5 -------------------------------------------------------------------------------- /JavaScript/math/math-imul.js: -------------------------------------------------------------------------------- 1 | console.log(Math.imul(3, 4)); 2 | // expected output: 12 3 | 4 | console.log(Math.imul(-5, 12)); 5 | // expected output: -60 6 | 7 | console.log(Math.imul(0xffffffff, 5)); 8 | // expected output: -5 9 | 10 | console.log(Math.imul(0xfffffffe, 5)); 11 | // expected output: -10 -------------------------------------------------------------------------------- /JavaScript/number/number-isinteger.js: -------------------------------------------------------------------------------- 1 | function fits(x, y) { 2 | if (Number.isInteger(y / x)) { 3 | return 'Fits!'; 4 | } 5 | return 'Does NOT fit!'; 6 | } 7 | 8 | console.log(fits(5, 10)); 9 | // expected output: "Fits!" 10 | 11 | console.log(fits(5, 11)); 12 | // expected output: "Does NOT fit!" -------------------------------------------------------------------------------- /JavaScript/object/object-defineproperty.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | Object.defineProperty(object1, 'property1', { 4 | value: 42, 5 | writable: false 6 | }); 7 | 8 | object1.property1 = 77; 9 | // throws an error in strict mode 10 | 11 | console.log(object1.property1); 12 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-replace.js: -------------------------------------------------------------------------------- 1 | class Replace1 { 2 | constructor(value) { 3 | this.value = value; 4 | } 5 | [Symbol.replace](string) { 6 | return `s/${string}/${this.value}/g`; 7 | } 8 | } 9 | 10 | console.log('foo'.replace(new Replace1('bar'))); 11 | // expected output: "s/foo/bar/g" -------------------------------------------------------------------------------- /JavaScript/weakset/weakset-prototype-has.js: -------------------------------------------------------------------------------- 1 | const weakset1 = new WeakSet(); 2 | const object1 = {}; 3 | const object2 = {}; 4 | 5 | weakset1.add(object1); 6 | 7 | console.log(weakset1.has(object1)); 8 | // expected output: true 9 | 10 | console.log(weakset1.has(object2)); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/date/date-getutcfullyear.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00'); 2 | const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00'); 3 | 4 | console.log(date1.getUTCFullYear()); 5 | // expected output: 1975 6 | 7 | console.log(date2.getUTCFullYear()); 8 | // expected output: 1976 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-objectinitializer.js: -------------------------------------------------------------------------------- 1 | var object1 = {a: 'foo', b: 42, c: {}}; 2 | 3 | console.log(object1.a); 4 | // expected output: "foo" 5 | 6 | var a = 'foo'; 7 | var b = 42; 8 | var c = {}; 9 | var object2 = {a: a, b: b, c: c}; 10 | 11 | console.log(object2.b); 12 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/math/math-tanh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.tanh(-1)); 2 | // expected output: -0.7615941559557649 3 | 4 | console.log(Math.tanh(0)); 5 | // expected output: 0 6 | 7 | console.log(Math.tanh(Infinity)); 8 | // expected output: 1 9 | 10 | console.log(Math.tanh(1)); 11 | // expected output: 0.7615941559557649 -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-lastindex.js: -------------------------------------------------------------------------------- 1 | var regex1 = new RegExp( "foo", "g" ); 2 | var str1 = 'table football, foosball'; 3 | 4 | regex1.test(str1); 5 | 6 | console.log(regex1.lastIndex); 7 | // expected output: 9 8 | 9 | regex1.test(str1); 10 | 11 | console.log(regex1.lastIndex); 12 | // expected output: 19 -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-entries.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | set1.add(42); 3 | set1.add('forty two'); 4 | 5 | const iterator1 = set1.entries(); 6 | 7 | for (let entry of iterator1) { 8 | console.log(entry); 9 | // expected output: [42, 42] 10 | // expected output: ["forty two", "forty two"] 11 | } -------------------------------------------------------------------------------- /JavaScript/statement/statement-return.js: -------------------------------------------------------------------------------- 1 | function getRectArea(width, height) { 2 | if (width > 0 && height > 0) { 3 | return width * height; 4 | } 5 | return 0; 6 | } 7 | 8 | console.log(getRectArea(3, 4)); 9 | // expected output: 12 10 | 11 | console.log(getRectArea(-3, 4)); 12 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/weakmap/weakmap-prototype-get.js: -------------------------------------------------------------------------------- 1 | const weakmap1 = new WeakMap(); 2 | const object1 = {}; 3 | const object2 = {}; 4 | 5 | weakmap1.set(object1, 42); 6 | 7 | console.log(weakmap1.get(object1)); 8 | // expected output: 42 9 | 10 | console.log(weakmap1.get(object2)); 11 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/weakmap/weakmap-prototype-has.js: -------------------------------------------------------------------------------- 1 | const weakmap1 = new WeakMap(); 2 | const object1 = {}; 3 | const object2 = {}; 4 | 5 | weakmap1.set(object1, 'foo'); 6 | 7 | console.log(weakmap1.has(object1)); 8 | // expected output: true 9 | 10 | console.log(weakmap1.has(object2)); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-join.js: -------------------------------------------------------------------------------- 1 | const elements = ['Fire', 'Air', 'Water']; 2 | 3 | console.log(elements.join()); 4 | // expected output: "Fire,Air,Water" 5 | 6 | console.log(elements.join('')); 7 | // expected output: "FireAirWater" 8 | 9 | console.log(elements.join('-')); 10 | // expected output: "Fire-Air-Water" -------------------------------------------------------------------------------- /JavaScript/array/array-sort.js: -------------------------------------------------------------------------------- 1 | const months = ['March', 'Jan', 'Feb', 'Dec']; 2 | months.sort(); 3 | console.log(months); 4 | // expected output: Array ["Dec", "Feb", "Jan", "March"] 5 | 6 | const array1 = [1, 30, 4, 21, 100000]; 7 | array1.sort(); 8 | console.log(array1); 9 | // expected output: Array [1, 100000, 21, 30, 4] -------------------------------------------------------------------------------- /JavaScript/date/date-setminutes.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | 3 | event.setMinutes(45); 4 | 5 | console.log(event.getMinutes()); 6 | // expected output: 45 7 | 8 | console.log(event); 9 | // expected output: Tue Aug 19 1975 23:45:30 GMT+0200 (CEST) 10 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-destructuringassignment.js: -------------------------------------------------------------------------------- 1 | var a, b, rest; 2 | [a, b] = [10, 20]; 3 | 4 | console.log(a); 5 | // expected output: 10 6 | 7 | console.log(b); 8 | // expected output: 20 9 | 10 | [a, b, ...rest] = [10, 20, 30, 40, 50]; 11 | 12 | console.log(rest); 13 | // expected output: [30,40,50] -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-set.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | Reflect.set(object1, 'property1', 42); 3 | 4 | console.log(object1.property1); 5 | // expected output: 42 6 | 7 | const array1 = ['duck', 'duck', 'duck']; 8 | Reflect.set(array1, 2, 'goose'); 9 | 10 | console.log(array1[2]); 11 | // expected output: "goose" -------------------------------------------------------------------------------- /JavaScript/array/array-includes.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 3]; 2 | 3 | console.log(array1.includes(2)); 4 | // expected output: true 5 | 6 | const pets = ['cat', 'dog', 'bat']; 7 | 8 | console.log(pets.includes('cat')); 9 | // expected output: true 10 | 11 | console.log(pets.includes('at')); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/function/function-call.js: -------------------------------------------------------------------------------- 1 | function Product(name, price) { 2 | this.name = name; 3 | this.price = price; 4 | } 5 | 6 | function Food(name, price) { 7 | Product.call(this, name, price); 8 | this.category = 'food'; 9 | } 10 | 11 | console.log(new Food('cheese', 5).name); 12 | // expected output: "cheese" -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-entries.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('0', 'foo'); 4 | map1.set(1, 'bar'); 5 | 6 | var iterator1 = map1.entries(); 7 | 8 | console.log(iterator1.next().value); 9 | // expected output: ["0", "foo"] 10 | 11 | console.log(iterator1.next().value); 12 | // expected output: [1, "bar"] -------------------------------------------------------------------------------- /JavaScript/math/math-abs.js: -------------------------------------------------------------------------------- 1 | function difference(a, b) { 2 | return Math.abs(a - b); 3 | } 4 | 5 | console.log(difference(3, 5)); 6 | // expected output: 2 7 | 8 | console.log(difference(5, 3)); 9 | // expected output: 2 10 | 11 | console.log(difference(1.23456, 7.89012)); 12 | // expected output: 6.6555599999999995 -------------------------------------------------------------------------------- /JavaScript/math/math-acosh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.acosh(0.999999999999)); 2 | // expected output: NaN 3 | 4 | console.log(Math.acosh(1)); 5 | // expected output: 0 6 | 7 | console.log(Math.acosh(2)); 8 | // expected output: 1.3169578969248166 9 | 10 | console.log(Math.acosh(2.5)); 11 | // expected output: 1.566799236972411 -------------------------------------------------------------------------------- /JavaScript/math/math-asinh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.asinh(1)); 2 | // expected output: 0.881373587019543 3 | 4 | console.log(Math.asinh(0)); 5 | // expected output: 0 6 | 7 | console.log(Math.asinh(-1)); 8 | // expected output: -0.881373587019543 9 | 10 | console.log(Math.asinh(2)); 11 | // expected output: 1.4436354751788103 -------------------------------------------------------------------------------- /JavaScript/math/math-atanh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.atanh(-1)); 2 | // expected output: -Infinity 3 | 4 | console.log(Math.atanh(0)); 5 | // expected output: 0 6 | 7 | console.log(Math.atanh(0.5)); 8 | // expected output: 0.549306144334055 (approximately) 9 | 10 | console.log(Math.atanh(1)); 11 | // expected output: Infinity -------------------------------------------------------------------------------- /JavaScript/math/math-expm1.js: -------------------------------------------------------------------------------- 1 | console.log(Math.expm1(0)); 2 | // expected output: 0 3 | 4 | console.log(Math.expm1(1)); 5 | // expected output: 1.718281828459045 6 | 7 | console.log(Math.expm1(-1)); 8 | // expected output: -0.6321205588285577 9 | 10 | console.log(Math.expm1(2)); 11 | // expected output: 6.38905609893065 -------------------------------------------------------------------------------- /JavaScript/math/math-fround.js: -------------------------------------------------------------------------------- 1 | console.log(Math.fround(5.5)); 2 | // expected output: 5.5 3 | 4 | console.log(Math.fround(5.05)); 5 | // expected output: 5.050000190734863 6 | 7 | console.log(Math.fround(5)); 8 | // expected output: 5 9 | 10 | console.log(Math.fround(-5.05)); 11 | // expected output: -5.050000190734863 -------------------------------------------------------------------------------- /JavaScript/math/math-sinh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.sinh(0)); 2 | // expected output: 0 3 | 4 | console.log(Math.sinh(1)); 5 | // expected output: 1.1752011936438014 6 | 7 | console.log(Math.sinh(-1)); 8 | // expected output: -1.1752011936438014 9 | 10 | console.log(Math.sinh(2)); 11 | // expected output: 3.626860407847019 -------------------------------------------------------------------------------- /JavaScript/math/math-sqrt.js: -------------------------------------------------------------------------------- 1 | function calcHypotenuse(a, b) { 2 | return(Math.sqrt((a * a) + (b * b))); 3 | } 4 | 5 | console.log(calcHypotenuse(3, 4)); 6 | // expected output: 5 7 | 8 | console.log(calcHypotenuse(5, 12)); 9 | // expected output: 13 10 | 11 | console.log(calcHypotenuse(0, 0)); 12 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-ownkeys.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42, 3 | property2: 13 4 | }; 5 | 6 | var array1 = []; 7 | 8 | console.log(Reflect.ownKeys(object1)); 9 | // expected output: Array ["property1", "property2"] 10 | 11 | console.log(Reflect.ownKeys(array1)); 12 | // expected output: Array ["length"] -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-delete.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | set1.add({x: 10, y: 20}).add({x: 20, y: 30}); 3 | 4 | // Delete any point with `x > 10`. 5 | set1.forEach(function(point){ 6 | if (point.x > 10) { 7 | set1.delete(point); 8 | } 9 | }); 10 | 11 | console.log(set1.size); 12 | // expected output: 1 -------------------------------------------------------------------------------- /JavaScript/string/string-matchall.js: -------------------------------------------------------------------------------- 1 | let regexp = /t(e)(st(\d?))/g; 2 | let str = 'test1test2'; 3 | 4 | let array = [...str.matchAll(regexp)]; 5 | 6 | console.log(array[0]); 7 | // expected output: Array ["test1", "e", "st1", "1"] 8 | 9 | console.log(array[1]); 10 | // expected output: Array ["test2", "e", "st2", "2"] -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-for.js: -------------------------------------------------------------------------------- 1 | console.log(Symbol.for('bar') === Symbol.for('bar')); 2 | // expected output: true 3 | 4 | console.log(Symbol('bar') === Symbol('bar')); 5 | // expected output: false 6 | 7 | const symbol1 = Symbol.for('foo'); 8 | 9 | console.log(symbol1.toString()); 10 | // expected output: "Symbol(foo)" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-join.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | 3 | console.log(uint8.join()); 4 | // expected output: "10,20,30,40,50" 5 | 6 | console.log(uint8.join('')); 7 | // expected output: "1020304050" 8 | 9 | console.log(uint8.join('-')); 10 | // expected output: "10-20-30-40-50" -------------------------------------------------------------------------------- /JavaScript/weakset/weakset-prototype-delete.js: -------------------------------------------------------------------------------- 1 | const weakset1 = new WeakSet(); 2 | const object1 = {}; 3 | 4 | weakset1.add(object1); 5 | 6 | console.log(weakset1.has(object1)); 7 | // expected output: true 8 | 9 | weakset1.delete(object1); 10 | 11 | console.log(weakset1.has(object1)); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-tolocalestring.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')]; 2 | const localeString = array1.toLocaleString('en', {timeZone: "UTC"}); 3 | 4 | console.log(localeString); 5 | // expected output: "1,a,12/21/1997, 2:12:00 PM", 6 | // This assumes "en" locale and UTC timezone - your results may vary -------------------------------------------------------------------------------- /JavaScript/date/date-getutcday.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00'); 2 | const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00'); 3 | 4 | // Tuesday 5 | console.log(date1.getUTCDay()); 6 | // expected output: 2 7 | 8 | // Wednesday 9 | console.log(date2.getUTCDay()); 10 | // expected output: 3 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-classexpression.js: -------------------------------------------------------------------------------- 1 | var Rectangle = class { 2 | constructor(height, width) { 3 | this.height = height; 4 | this.width = width; 5 | } 6 | area() { 7 | return this.height * this.width; 8 | } 9 | } 10 | 11 | console.log(new Rectangle(5,8).area()); 12 | // expected output: 40 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-groupingoperator.js: -------------------------------------------------------------------------------- 1 | console.log(1 + 2 * 3); // 1 + 6 2 | // expected output: 7 3 | 4 | console.log(1 + (2 * 3)); // 1 + 6 5 | // expected output: 7 6 | 7 | console.log((1 + 2) * 3); // 3 * 3 8 | // expected output: 9 9 | 10 | console.log(1 * 3 + 2 * 3); // 3 + 6 11 | // expected output: 9 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-isnan.js: -------------------------------------------------------------------------------- 1 | function milliseconds(x) { 2 | if (isNaN(x)) { 3 | return 'Not a Number!'; 4 | } 5 | return x * 1000; 6 | } 7 | 8 | console.log(milliseconds('100F')); 9 | // expected output: "Not a Number!" 10 | 11 | console.log(milliseconds('0.0314E+2')); 12 | // expected output: 3140 -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-@@iterator.js: -------------------------------------------------------------------------------- 1 | var map1 = new Map(); 2 | 3 | map1.set('0', 'foo'); 4 | map1.set(1, 'bar'); 5 | 6 | var iterator1 = map1[Symbol.iterator](); 7 | 8 | for (let item of iterator1) { 9 | console.log(item); 10 | // expected output: Array ["0", "foo"] 11 | // expected output: Array [1, "bar"] 12 | } -------------------------------------------------------------------------------- /JavaScript/math/math-exp.js: -------------------------------------------------------------------------------- 1 | console.log(Math.exp(0)); 2 | // expected output: 1 3 | 4 | console.log(Math.exp(1)); 5 | // expected output: 2.718281828459 (approximately) 6 | 7 | console.log(Math.exp(-1)); 8 | // expected output: 0.36787944117144233 9 | 10 | console.log(Math.exp(2)); 11 | // expected output: 7.38905609893065 -------------------------------------------------------------------------------- /JavaScript/object/object-getownpropertydescriptor.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | } 4 | 5 | const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1'); 6 | 7 | console.log(descriptor1.configurable); 8 | // expected output: true 9 | 10 | console.log(descriptor1.value); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-@@iterator.js: -------------------------------------------------------------------------------- 1 | const set1 = new Set(); 2 | 3 | set1.add(42); 4 | set1.add('forty two'); 5 | 6 | const iterator1 = set1[Symbol.iterator](); 7 | 8 | console.log(iterator1.next().value); 9 | // expected output: 42 10 | 11 | console.log(iterator1.next().value); 12 | // expected output: "forty two" -------------------------------------------------------------------------------- /JavaScript/statement/statement-throw.js: -------------------------------------------------------------------------------- 1 | function getRectArea(width, height) { 2 | if (isNaN(width) || isNaN(height)) { 3 | throw "Parameter is not a number!"; 4 | } 5 | } 6 | 7 | try { 8 | getRectArea(3, 'A'); 9 | } 10 | catch(e) { 11 | console.error(e); 12 | // expected output: "Parameter is not a number!" 13 | } -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-indexof.js: -------------------------------------------------------------------------------- 1 | const uint8 = new Uint8Array([10, 20, 30, 40, 50]); 2 | 3 | console.log(uint8.indexOf(50)); 4 | // expected output: 4 5 | 6 | // from position 3 7 | console.log(uint8.indexOf(20, 3)); 8 | // expected output: -1 9 | 10 | console.log(uint8.indexOf(51)); 11 | // expected output: -1 -------------------------------------------------------------------------------- /JavaScript/date/date-utc.js: -------------------------------------------------------------------------------- 1 | const utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5)); 2 | const utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0)); 3 | 4 | console.log(utcDate1.toUTCString()); 5 | // expected output: Fri, 02 Feb 1996 03:04:05 GMT 6 | 7 | console.log(utcDate2.toUTCString()); 8 | // expected output: Sun, 31 Dec 1899 00:00:00 GMT -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-inoperator.js: -------------------------------------------------------------------------------- 1 | var car = {make: 'Honda', model: 'Accord', year: 1998}; 2 | 3 | console.log('make' in car); 4 | // expected output: true 5 | 6 | delete car.make; 7 | if ('make' in car === false) { 8 | car.make = 'Suzuki'; 9 | } 10 | 11 | console.log(car.make); 12 | // expected output: "Suzuki" -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-parseint.js: -------------------------------------------------------------------------------- 1 | function roughScale(x, base) { 2 | const parsed = parseInt(x, base); 3 | if (isNaN(parsed)) { return 0 } 4 | return parsed * 100; 5 | } 6 | 7 | console.log(roughScale(' 0xF', 16)); 8 | // expected output: 1500 9 | 10 | console.log(roughScale('321', 2)); 11 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/number/number-isnan.js: -------------------------------------------------------------------------------- 1 | function typeOfNaN(x) { 2 | if (Number.isNaN(x)) { 3 | return 'Number NaN'; 4 | } 5 | if (isNaN(x)) { 6 | return 'NaN'; 7 | } 8 | } 9 | 10 | console.log(typeOfNaN('100F')); 11 | // expected output: "NaN" 12 | 13 | console.log(typeOfNaN(NaN)); 14 | // expected output: "Number NaN" -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-tolocalestring.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); 2 | 3 | console.log(date1.toLocaleString('ar-EG')); 4 | // expected output: "٢٠‏/١٢‏/٢٠١٢ ٤:٠٠:٠٠ ص" 5 | 6 | const number1 = 123456.789; 7 | 8 | console.log(number1.toLocaleString('de-DE')); 9 | // expected output: "123.456,789" -------------------------------------------------------------------------------- /JavaScript/string/string-raw.js: -------------------------------------------------------------------------------- 1 | // Create a variable that uses a Windows 2 | // path without escaping the backslashes: 3 | const filePath = String.raw`C:\Development\profile\aboutme.html`; 4 | 5 | console.log('The file was uploaded from: ' + filePath); 6 | // expected output: "The file was uploaded from: C:\Development\profile\aboutme.html" -------------------------------------------------------------------------------- /JavaScript/array/array-reduce.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 3, 4]; 2 | const reducer = (accumulator, currentValue) => accumulator + currentValue; 3 | 4 | // 1 + 2 + 3 + 4 5 | console.log(array1.reduce(reducer)); 6 | // expected output: 10 7 | 8 | // 5 + 1 + 2 + 3 + 4 9 | console.log(array1.reduce(reducer, 5)); 10 | // expected output: 15 -------------------------------------------------------------------------------- /JavaScript/date/date-getutcmonth.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00'); 2 | const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00'); 3 | 4 | // December 5 | console.log(date1.getUTCMonth()); 6 | // expected output: 11 7 | 8 | // January 9 | console.log(date2.getUTCMonth()); 10 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/math/math-acos.js: -------------------------------------------------------------------------------- 1 | // Calculates angle of a right-angle triangle in radians 2 | function calcAngle(adjacent, hypotenuse) { 3 | return Math.acos(adjacent / hypotenuse); 4 | } 5 | 6 | console.log(calcAngle(8, 10)); 7 | // expected output: 0.6435011087932843 8 | 9 | console.log(calcAngle(5, 3)); 10 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/math/math-asin.js: -------------------------------------------------------------------------------- 1 | // Calculates angle of a right-angle triangle in radians 2 | function calcAngle(opposite, hypotenuse) { 3 | return Math.asin(opposite / hypotenuse); 4 | } 5 | 6 | console.log(calcAngle(6, 10)); 7 | // expected output: 0.6435011087932844 8 | 9 | console.log(calcAngle(5, 3)); 10 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/math/math-atan2.js: -------------------------------------------------------------------------------- 1 | function calcAngleDegrees(x, y) { 2 | return Math.atan2(y, x) * 180 / Math.PI; 3 | } 4 | 5 | console.log(calcAngleDegrees(5, 5)); 6 | //expected output: 45 7 | 8 | console.log(calcAngleDegrees(10, 10)); 9 | //expected output: 45 10 | 11 | console.log(calcAngleDegrees(0, 10)); 12 | //expected output: 90 -------------------------------------------------------------------------------- /JavaScript/math/math-clz32.js: -------------------------------------------------------------------------------- 1 | // 00000000000000000000000000000001 2 | console.log(Math.clz32(1)); 3 | // expected output: 31 4 | 5 | // 00000000000000000000000000000100 6 | console.log(Math.clz32(4)); 7 | // expected output: 29 8 | 9 | // 00000000000000000000001111101000 10 | console.log(Math.clz32(1000)); 11 | // expected output: 22 -------------------------------------------------------------------------------- /JavaScript/math/math-pow.js: -------------------------------------------------------------------------------- 1 | console.log(Math.pow(7, 3)); 2 | // expected output: 343 3 | 4 | console.log(Math.pow(4, 0.5)); 5 | // expected output: 2 6 | 7 | console.log(Math.pow(7, -2)); 8 | // expected output: 0.02040816326530612 9 | // (1/49) 10 | 11 | console.log(Math.pow(-7, 0.5)); 12 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/number/number-max-safe-integer.js: -------------------------------------------------------------------------------- 1 | var x = Number.MAX_SAFE_INTEGER + 1; 2 | var y = Number.MAX_SAFE_INTEGER + 2; 3 | 4 | console.log(Number.MAX_SAFE_INTEGER); 5 | // expected output: 9007199254740991 6 | 7 | console.log(x); 8 | // expected output: 9007199254740992 9 | 10 | console.log(x === y); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/number/number-min-safe-integer.js: -------------------------------------------------------------------------------- 1 | var x = Number.MIN_SAFE_INTEGER - 1; 2 | var y = Number.MIN_SAFE_INTEGER - 2; 3 | 4 | console.log(Number.MIN_SAFE_INTEGER); 5 | // expected output: -9007199254740991 6 | 7 | console.log(x); 8 | // expected output: -9007199254740992 9 | 10 | console.log(x === y); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/number/number-toexponential.js: -------------------------------------------------------------------------------- 1 | function expo(x, f) { 2 | return Number.parseFloat(x).toExponential(f); 3 | } 4 | 5 | console.log(expo(123456, 2)); 6 | // expected output: "1.23e+5" 7 | 8 | console.log(expo('123456')); 9 | // expected output: "1.23456e+5" 10 | 11 | console.log(expo('oink')); 12 | // expected output: "NaN" -------------------------------------------------------------------------------- /JavaScript/number/number-tofixed.js: -------------------------------------------------------------------------------- 1 | function financial(x) { 2 | return Number.parseFloat(x).toFixed(2); 3 | } 4 | 5 | console.log(financial(123.456)); 6 | // expected output: "123.46" 7 | 8 | console.log(financial(0.004)); 9 | // expected output: "0.00" 10 | 11 | console.log(financial('1.23e+5')); 12 | // expected output: "123000.00" -------------------------------------------------------------------------------- /JavaScript/object/object-assign.js: -------------------------------------------------------------------------------- 1 | const target = { a: 1, b: 2 }; 2 | const source = { b: 4, c: 5 }; 3 | 4 | const returnedTarget = Object.assign(target, source); 5 | 6 | console.log(target); 7 | // expected output: Object { a: 1, b: 4, c: 5 } 8 | 9 | console.log(returnedTarget); 10 | // expected output: Object { a: 1, b: 4, c: 5 } -------------------------------------------------------------------------------- /JavaScript/object/object-getownpropertydescriptors.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | const descriptors1 = Object.getOwnPropertyDescriptors(object1); 6 | 7 | console.log(descriptors1.property1.writable); 8 | // expected output: true 9 | 10 | console.log(descriptors1.property1.value); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/object/object-getownpropertysymbols.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | const a = Symbol('a'); 3 | const b = Symbol.for('b'); 4 | 5 | object1[a] = 'localSymbol'; 6 | object1[b] = 'globalSymbol'; 7 | 8 | const objectSymbols = Object.getOwnPropertySymbols(object1); 9 | 10 | console.log(objectSymbols.length); 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-seal.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | Object.seal(object1); 6 | object1.property1 = 33; 7 | console.log(object1.property1); 8 | // expected output: 33 9 | 10 | delete object1.property1; // cannot delete when sealed 11 | console.log(object1.property1); 12 | // expected output: 33 -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-has.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | console.log(Reflect.has(object1, 'property1')); 6 | // expected output: true 7 | 8 | console.log(Reflect.has(object1, 'property2')); 9 | // expected output: false 10 | 11 | console.log(Reflect.has(object1, 'toString')); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-@@search.js: -------------------------------------------------------------------------------- 1 | class RegExp1 extends RegExp { 2 | constructor(str) { 3 | super(str) 4 | this.pattern = str; 5 | } 6 | [Symbol.search](str) { 7 | return str.indexOf(this.pattern); 8 | } 9 | } 10 | 11 | console.log('table football'.search(new RegExp1('foo'))); 12 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/set/set-prototype-foreach.js: -------------------------------------------------------------------------------- 1 | function logSetElements(value1, value2, set) { 2 | console.log('s[' + value1 + '] = ' + value2); 3 | } 4 | 5 | new Set(['foo', 'bar', undefined]).forEach(logSetElements); 6 | 7 | // expected output: "s[foo] = foo" 8 | // expected output: "s[bar] = bar" 9 | // expected output: "s[undefined] = undefined" -------------------------------------------------------------------------------- /JavaScript/statement/statement-const.js: -------------------------------------------------------------------------------- 1 | const number = 42; 2 | 3 | try { 4 | number = 99; 5 | } catch(err) { 6 | console.log(err); 7 | // expected output: TypeError: invalid assignment to const `number' 8 | // Note - error messages will vary depending on browser 9 | } 10 | 11 | console.log(number); 12 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-set.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(8); 3 | const uint8 = new Uint8Array(buffer); 4 | 5 | // Copy the values into the array starting at index 3 6 | uint8.set([1, 2, 3], 3); 7 | 8 | console.log(uint8); 9 | // expected output: Uint8Array [0, 0, 0, 1, 2, 3, 0, 0] -------------------------------------------------------------------------------- /JavaScript/array/array-indexof.js: -------------------------------------------------------------------------------- 1 | const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; 2 | 3 | console.log(beasts.indexOf('bison')); 4 | // expected output: 1 5 | 6 | // start from index 2 7 | console.log(beasts.indexOf('bison', 2)); 8 | // expected output: 4 9 | 10 | console.log(beasts.indexOf('giraffe')); 11 | // expected output: -1 -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-store.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 5; 5 | 6 | console.log(Atomics.store(uint8, 0, 2)); 7 | // expected output: 2 8 | 9 | console.log(Atomics.load(uint8, 0)); 10 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-bytelength.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | const view1 = new DataView(buffer); 5 | const view2 = new DataView(buffer,12,4); //from byte 12 for the next 4 bytes 6 | 7 | console.log(view1.byteLength + view2.byteLength); // 16 + 4 8 | // expected output: 20 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-conditionaloperators.js: -------------------------------------------------------------------------------- 1 | function getFee(isMember) { 2 | return (isMember ? "$2.00" : "$10.00"); 3 | } 4 | 5 | console.log(getFee(true)); 6 | // expected output: "$2.00" 7 | 8 | console.log(getFee(false)); 9 | // expected output: "$10.00" 10 | 11 | console.log(getFee(1)); 12 | // expected output: "$2.00" -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-typeof.js: -------------------------------------------------------------------------------- 1 | console.log(typeof 42); 2 | // expected output: "number" 3 | 4 | console.log(typeof 'blubber'); 5 | // expected output: "string" 6 | 7 | console.log(typeof true); 8 | // expected output: "boolean" 9 | 10 | console.log(typeof declaredButUndefinedVariable); 11 | // expected output: "undefined"; -------------------------------------------------------------------------------- /JavaScript/function/function-tostring.js: -------------------------------------------------------------------------------- 1 | function sum(a, b) { 2 | return a + b; 3 | } 4 | 5 | console.log(sum.toString()); 6 | // expected output: "function sum(a, b) { 7 | // return a + b; 8 | // }" 9 | 10 | console.log(Math.abs.toString()); 11 | // expected output: "function abs() { [native code] }" -------------------------------------------------------------------------------- /JavaScript/number/number-tolocalestring.js: -------------------------------------------------------------------------------- 1 | function eArabic(x){ 2 | return x.toLocaleString('ar-EG'); 3 | } 4 | 5 | console.log(eArabic(123456.789)); 6 | // expected output: "١٢٣٬٤٥٦٫٧٨٩" 7 | 8 | console.log(eArabic("123456.789")); 9 | // expected output: "123456.789" 10 | 11 | console.log(eArabic(NaN)); 12 | // expected output: "ليس رقم" -------------------------------------------------------------------------------- /JavaScript/number/number-toprecision.js: -------------------------------------------------------------------------------- 1 | function precise(x) { 2 | return Number.parseFloat(x).toPrecision(4); 3 | } 4 | 5 | console.log(precise(123.456)); 6 | // expected output: "123.5" 7 | 8 | console.log(precise(0.004)); 9 | // expected output: "0.004000" 10 | 11 | console.log(precise('1.23e+5')); 12 | // expected output: "1.230e+5" -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-propertyaccessors.js: -------------------------------------------------------------------------------- 1 | var person = {}; 2 | person['firstname'] = 'Mario'; 3 | person['lastname'] = 'Rossi'; 4 | 5 | console.log(person.firstname); 6 | // expected output: "Mario" 7 | 8 | person = {'firstname': 'John', 'lastname': 'Doe'} 9 | 10 | console.log(person['lastname']); 11 | // expected output: "Doe" -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-isfinite.js: -------------------------------------------------------------------------------- 1 | function div(x) { 2 | if (isFinite(1000 / x)) { 3 | return 'Number is NOT Infinity.'; 4 | } 5 | return "Number is Infinity!"; 6 | } 7 | 8 | console.log(div(0)); 9 | // expected output: "Number is Infinity!"" 10 | 11 | console.log(div(1)); 12 | // expected output: "Number is NOT Infinity." -------------------------------------------------------------------------------- /JavaScript/math/math-atan.js: -------------------------------------------------------------------------------- 1 | // Calculates angle of a right-angle triangle in radians 2 | function calcAngle(opposite, adjacent) { 3 | return Math.atan(opposite / adjacent); 4 | } 5 | 6 | console.log(calcAngle(8, 10)); 7 | // expected output: 0.6747409422235527 8 | 9 | console.log(calcAngle(5, 3)); 10 | // expected output: 1.0303768265243125 -------------------------------------------------------------------------------- /JavaScript/math/math-random.js: -------------------------------------------------------------------------------- 1 | function getRandomInt(max) { 2 | return Math.floor(Math.random() * Math.floor(max)); 3 | } 4 | 5 | console.log(getRandomInt(3)); 6 | // expected output: 0, 1 or 2 7 | 8 | console.log(getRandomInt(1)); 9 | // expected output: 0 10 | 11 | console.log(Math.random()); 12 | // expected output: a number between 0 and 1 -------------------------------------------------------------------------------- /JavaScript/object/object-preventextensions.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | Object.preventExtensions(object1); 4 | 5 | try { 6 | Object.defineProperty(object1, 'property1', { 7 | value: 42 8 | }); 9 | } catch (e) { 10 | console.log(e); 11 | // Expected output: TypeError: Cannot define property property1, object is not extensible 12 | } -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-construct.js: -------------------------------------------------------------------------------- 1 | function func1(a, b, c) { 2 | this.sum = a + b + c; 3 | } 4 | 5 | const args = [1, 2, 3]; 6 | const object1 = new func1(...args); 7 | const object2 = Reflect.construct(func1, args); 8 | 9 | console.log(object2.sum); 10 | // expected output: 6 11 | 12 | console.log(object1.sum); 13 | // expected output: 6 -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-getregexp-@@species.js: -------------------------------------------------------------------------------- 1 | class MyRegExp extends RegExp { 2 | // Overwrite MyRegExp species to the parent RegExp constructor 3 | static get [Symbol.species]() { 4 | return RegExp; 5 | } 6 | } 7 | 8 | const regex1 = new MyRegExp('foo','g'); 9 | 10 | console.log(regex1.test('football')); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-ignorecase.js: -------------------------------------------------------------------------------- 1 | var regex1 = new RegExp('foo'); 2 | var regex2 = new RegExp('foo', 'i'); 3 | 4 | console.log(regex1.test('Football')); 5 | // expected output: false 6 | 7 | console.log(regex2.ignoreCase); 8 | // expected output: true 9 | 10 | console.log(regex2.test('Football')); 11 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-source.js: -------------------------------------------------------------------------------- 1 | var regex1 = /fooBar/ig; 2 | 3 | console.log(regex1.source); 4 | // expected output: "fooBar" 5 | 6 | console.log(new RegExp().source); 7 | // expected output: "(?:)" 8 | 9 | console.log(new RegExp('\n').source === '\\n'); 10 | // expected output: true (starting with ES5) 11 | // (due to escaping) -------------------------------------------------------------------------------- /JavaScript/weakmap/weakmap-prototype-set.js: -------------------------------------------------------------------------------- 1 | const weakmap1 = new WeakMap(); 2 | const object1 = {}; 3 | const object2 = {}; 4 | 5 | weakmap1.set(object1, 'foo'); 6 | weakmap1.set(object2, 'bar'); 7 | 8 | console.log(weakmap1.get(object1)); 9 | //expected output: "foo" 10 | 11 | console.log(weakmap1.get(object2)); 12 | //expected output: "bar" -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-add.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 7; 5 | 6 | // 7 + 2 = 9 7 | console.log(Atomics.add(uint8, 0, 2)); 8 | // expected output: 7 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 9 -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-sub.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 7; 5 | 6 | // 7 - 2 = 5 7 | console.log(Atomics.sub(uint8, 0, 2)); 8 | // expected output: 7 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 5 -------------------------------------------------------------------------------- /JavaScript/date/date-getutcminutes.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('1 January 2000 03:15:30 GMT+07:00'); 2 | const date2 = new Date('1 January 2000 03:15:30 GMT+03:30'); 3 | 4 | console.log(date1.getUTCMinutes()); // 31 Dec 1999 20:15:30 GMT 5 | // expected output: 15 6 | 7 | console.log(date2.getUTCMinutes()); // 31 Dec 1999 23:45:30 GMT 8 | // expected output: 45 -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-eval.js: -------------------------------------------------------------------------------- 1 | console.log(eval('2 + 2')); 2 | // expected output: 4 3 | 4 | console.log(eval(new String('2 + 2'))); 5 | // expected output: 2 + 2 6 | 7 | console.log(eval('2 + 2') === eval('4')); 8 | // expected output: true 9 | 10 | console.log(eval('2 + 2') === eval(new String('2 + 2'))); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/math/math-cosh.js: -------------------------------------------------------------------------------- 1 | console.log(Math.cosh(0)); 2 | // expected output: 1 3 | 4 | console.log(Math.cosh(1)); 5 | // expected output: 1.543080634815244 (approximately) 6 | 7 | console.log(Math.cosh(-1)); 8 | // expected output: 1.543080634815244 (approximately) 9 | 10 | console.log(Math.cosh(2)); 11 | // expected output: 3.7621956910836314 -------------------------------------------------------------------------------- /JavaScript/number/number-parseint.js: -------------------------------------------------------------------------------- 1 | function roughScale(x, base) { 2 | var parsed = Number.parseInt(x, base); 3 | if (Number.isNaN(parsed)) { 4 | return 0; 5 | } 6 | return parsed * 100; 7 | } 8 | 9 | console.log(roughScale(' 0xF', 16)); 10 | // expected output: 1500 11 | 12 | console.log(roughScale('321', 2)); 13 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/promise/promise-all.js: -------------------------------------------------------------------------------- 1 | var promise1 = Promise.resolve(3); 2 | var promise2 = 42; 3 | var promise3 = new Promise(function(resolve, reject) { 4 | setTimeout(resolve, 100, 'foo'); 5 | }); 6 | 7 | Promise.all([promise1, promise2, promise3]).then(function(values) { 8 | console.log(values); 9 | }); 10 | // expected output: Array [3, 42, "foo"] -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-sticky.js: -------------------------------------------------------------------------------- 1 | var str1 = 'table football'; 2 | var regex1 = new RegExp('foo','y'); 3 | 4 | regex1.lastIndex = 6; 5 | 6 | console.log(regex1.sticky); 7 | // expected output: true 8 | 9 | console.log(regex1.test(str1)); 10 | // expected output: true 11 | 12 | console.log(regex1.test(str1)); 13 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-load.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 5; 5 | 6 | // 5 + 2 = 7 7 | console.log(Atomics.add(uint8, 0, 2)); 8 | // expected output: 5 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 7 -------------------------------------------------------------------------------- /JavaScript/date/date-constructor.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('December 17, 1995 03:24:00'); 2 | // Sun Dec 17 1995 03:24:00 GMT... 3 | 4 | const date2 = new Date('1995-12-17T03:24:00'); 5 | // Sun Dec 17 1995 03:24:00 GMT... 6 | 7 | console.log(date1 === date2); 8 | // expected output: false; 9 | 10 | console.log(date1 - date2); 11 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/math/math-cos.js: -------------------------------------------------------------------------------- 1 | function getCircleX(radians, radius) { 2 | return Math.cos(radians) * radius; 3 | } 4 | 5 | console.log(getCircleX(1, 10)); 6 | // expected output: 5.403023058681398 7 | 8 | console.log(getCircleX(2, 10)); 9 | // expected output: -4.161468365471424 10 | 11 | console.log(getCircleX(Math.PI, 10)); 12 | // expected output: -10 -------------------------------------------------------------------------------- /JavaScript/number/number-min-value.js: -------------------------------------------------------------------------------- 1 | function multiply(x, y) { 2 | if (x * y < Number.MIN_VALUE) { 3 | return "Process as -Infinity"; 4 | } 5 | return (x * y); 6 | } 7 | 8 | console.log(multiply(5e-324, 1)); 9 | // expected output: 5e-324 10 | 11 | console.log(multiply(-1.7976931348623157e+308, 2)); 12 | // expected output: Process as -Infinity -------------------------------------------------------------------------------- /JavaScript/promise/promise-constructor.js: -------------------------------------------------------------------------------- 1 | var promise1 = new Promise(function(resolve, reject) { 2 | setTimeout(function() { 3 | resolve('foo'); 4 | }, 300); 5 | }); 6 | 7 | promise1.then(function(value) { 8 | console.log(value); 9 | // expected output: "foo" 10 | }); 11 | 12 | console.log(promise1); 13 | // expected output: [object Promise] -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-species.js: -------------------------------------------------------------------------------- 1 | class Array1 extends Array { 2 | static get [Symbol.species]() { return Array; } 3 | } 4 | 5 | const a = new Array1(1, 2, 3); 6 | const mapped = a.map(x => x * x); 7 | 8 | console.log(mapped instanceof Array1); 9 | // expected output: false 10 | 11 | console.log(mapped instanceof Array); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-arithmetic.js: -------------------------------------------------------------------------------- 1 | // Number addition and subtraction 2 | console.log(2 + 3 - 1); 3 | // expected output: 4 4 | 5 | // Number multiplication and division 6 | console.log(4 * 3 / 2); // 12 / 2 7 | // expected output: 6 8 | 9 | // Number remainder and exponential 10 | console.log(11 % 3 ** 2); // 11 % 9 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-instanceof.js: -------------------------------------------------------------------------------- 1 | function Car(make, model, year) { 2 | this.make = make; 3 | this.model = model; 4 | this.year = year; 5 | } 6 | var auto = new Car('Honda', 'Accord', 1998); 7 | 8 | console.log(auto instanceof Car); 9 | // expected output: true 10 | 11 | console.log(auto instanceof Object); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/math/math-tan.js: -------------------------------------------------------------------------------- 1 | function getTanFromDegrees(degrees) { 2 | return Math.tan(degrees * Math.PI/180); 3 | } 4 | 5 | console.log(getTanFromDegrees(0)); 6 | // expected output: 0 7 | 8 | console.log(getTanFromDegrees(45)); 9 | // expected output: 0.9999999999999999 10 | 11 | console.log(getTanFromDegrees(90)); 12 | // expected output: 16331239353195370 -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-defineproperty.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | if (Reflect.defineProperty(object1, 'property1', {value: 42})) { 4 | console.log('property1 created!'); 5 | // expected output: "property1 created!" 6 | } else { 7 | console.log('problem creating property1'); 8 | } 9 | 10 | console.log(object1.property1); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/string/string-lastindexof.js: -------------------------------------------------------------------------------- 1 | var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; 2 | 3 | var searchTerm = 'dog'; 4 | 5 | console.log('The index of the first "' + searchTerm + '" from the end is ' + paragraph.lastIndexOf(searchTerm)); 6 | // expected output: "The index of the first "dog" from the end is 52" -------------------------------------------------------------------------------- /JavaScript/date/date-settime.js: -------------------------------------------------------------------------------- 1 | const event1 = new Date('July 1, 1999'); 2 | const event2 = new Date(); 3 | event2.setTime(event1.getTime()); 4 | 5 | console.log(event1); 6 | // expected output: Thu Jul 01 1999 00:00:00 GMT+0200 (CEST) 7 | 8 | console.log(event2); 9 | // expected output: Thu Jul 01 1999 00:00:00 GMT+0200 (CEST) 10 | // (note: your timezone may vary) -------------------------------------------------------------------------------- /JavaScript/map/map-prototype-foreach.js: -------------------------------------------------------------------------------- 1 | function logMapElements(value, key, map) { 2 | console.log(`m[${key}] = ${value}`); 3 | } 4 | 5 | new Map([['foo', 3], ['bar', {}], ['baz', undefined]]) 6 | .forEach(logMapElements); 7 | 8 | // expected output: "m[foo] = 3" 9 | // expected output: "m[bar] = [object Object]" 10 | // expected output: "m[baz] = undefined" -------------------------------------------------------------------------------- /JavaScript/number/number-parsefloat.js: -------------------------------------------------------------------------------- 1 | function circumference(r) { 2 | if (Number.isNaN(Number.parseFloat(r))) { 3 | return 0; 4 | } 5 | return parseFloat(r) * 2.0 * Math.PI ; 6 | } 7 | 8 | console.log(circumference('4.567abcdefgh')); 9 | // expected output: 28.695307297889173 10 | 11 | console.log(circumference('abcdefgh')); 12 | // expected output: 0 -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-or.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 5; 5 | 6 | // 5 (0101) OR 2 (0010) = 7 (0111) 7 | console.log(Atomics.or(uint8, 0, 2)); 8 | // expected output: 5 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 7 -------------------------------------------------------------------------------- /JavaScript/date/date-sethours.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30'); 2 | event.setHours(20); 3 | 4 | console.log(event); 5 | // expected output: Tue Aug 19 1975 20:15:30 GMT+0200 (CEST) 6 | // (note: your timezone may vary) 7 | 8 | event.setHours(20,21,22); 9 | 10 | console.log(event); 11 | // expected output: Tue Aug 19 1975 20:21:22 GMT+0200 (CEST) -------------------------------------------------------------------------------- /JavaScript/number/number-issafeinteger.js: -------------------------------------------------------------------------------- 1 | function warn(x) { 2 | if (Number.isSafeInteger(x)) { 3 | return 'Precision safe.'; 4 | } 5 | return 'Precision may be lost!'; 6 | } 7 | 8 | console.log(warn(Math.pow(2, 53))); 9 | // expected output: "Precision may be lost!" 10 | 11 | console.log(warn(Math.pow(2, 53) - 1)); 12 | // expected output: "Precision safe." -------------------------------------------------------------------------------- /JavaScript/string/string-padstart.js: -------------------------------------------------------------------------------- 1 | const str1 = '5'; 2 | 3 | console.log(str1.padStart(2, '0')); 4 | // expected output: "05" 5 | 6 | const fullNumber = '2034399002125581'; 7 | const last4Digits = fullNumber.slice(-4); 8 | const maskedNumber = last4Digits.padStart(fullNumber.length, '*'); 9 | 10 | console.log(maskedNumber); 11 | // expected output: "************5581" -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-constructor.js: -------------------------------------------------------------------------------- 1 | const symbol1 = Symbol(); 2 | const symbol2 = Symbol(42); 3 | const symbol3 = Symbol('foo'); 4 | 5 | console.log(typeof symbol1); 6 | // expected output: "symbol" 7 | 8 | console.log(symbol3.toString()); 9 | // expected output: "Symbol(foo)" 10 | 11 | console.log(Symbol('foo') === Symbol('foo')); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-and.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 7; 5 | 6 | // 7 (0111) AND 2 (0010) = 2 (0010) 7 | console.log(Atomics.and(uint8, 0, 2)); 8 | // expected output: 7 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-xor.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 7; 5 | 6 | // 7 (0111) XOR 2 (0010) = 5 (0101) 7 | console.log(Atomics.xor(uint8, 0, 2)); 8 | // expected output: 7 9 | 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 5 -------------------------------------------------------------------------------- /JavaScript/date/date-setutcmonth.js: -------------------------------------------------------------------------------- 1 | const event = new Date('December 31, 1975 23:15:30 GMT-3:00'); 2 | 3 | console.log(event.toUTCString()); 4 | // Thu, 01 Jan 1976 02:15:30 GMT 5 | 6 | console.log(event.getUTCMonth()); 7 | // expected output: 0 8 | 9 | event.setUTCMonth(11); 10 | 11 | console.log(event.toUTCString()); 12 | // expected output: Wed, 01 Dec 1976 02:15:30 GMT -------------------------------------------------------------------------------- /JavaScript/math/math-sin.js: -------------------------------------------------------------------------------- 1 | function getCircleY(radians, radius) { 2 | return Math.sin(radians) * radius; 3 | } 4 | 5 | console.log(getCircleY(1, 10)); 6 | // expected output: 8.414709848078965 7 | 8 | console.log(getCircleY(2, 10)); 9 | // expected output: 9.092974268256818 10 | 11 | console.log(getCircleY(Math.PI, 10)); 12 | // expected output: 1.2246467991473533e-15 -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-deleteproperty.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | Reflect.deleteProperty(object1, 'property1'); 6 | 7 | console.log(object1.property1); 8 | // expected output: undefined 9 | 10 | var array1 = [1, 2, 3, 4, 5]; 11 | Reflect.deleteProperty(array1, '3'); 12 | 13 | console.log(array1); 14 | // expected output: Array [1, 2, 3, , 5] -------------------------------------------------------------------------------- /JavaScript/array/array-copywithin.js: -------------------------------------------------------------------------------- 1 | const array1 = ['a', 'b', 'c', 'd', 'e']; 2 | 3 | // copy to index 0 the element at index 3 4 | console.log(array1.copyWithin(0, 3, 4)); 5 | // expected output: Array ["d", "b", "c", "d", "e"] 6 | 7 | // copy to index 1 all elements from index 3 to the end 8 | console.log(array1.copyWithin(1, 3)); 9 | // expected output: Array ["d", "d", "e", "d", "e"] -------------------------------------------------------------------------------- /JavaScript/array/array-fill.js: -------------------------------------------------------------------------------- 1 | const array1 = [1, 2, 3, 4]; 2 | 3 | // fill with 0 from position 2 until position 4 4 | console.log(array1.fill(0, 2, 4)); 5 | // expected output: [1, 2, 0, 0] 6 | 7 | // fill with 5 from position 1 8 | console.log(array1.fill(5, 1)); 9 | // expected output: [1, 5, 5, 5] 10 | 11 | console.log(array1.fill(6)); 12 | // expected output: [6, 6, 6, 6] -------------------------------------------------------------------------------- /JavaScript/expressions/expressions-bitwiseoperators.js: -------------------------------------------------------------------------------- 1 | console.log(5 & 13); // 0101 & 1101 = 0101 2 | // expected output: 5; 3 | 4 | console.log(parseInt("0101",2) & parseInt("1101",2)); 5 | // expected output: 5; 6 | 7 | console.log(5 & 13 & 3); // 0101 & 1101 & 0011 = 0001 8 | // expected output: 1; 9 | 10 | console.log(5 | 13); // 0101 | 1101 = 1101 11 | // expected output: 13 -------------------------------------------------------------------------------- /JavaScript/intl/intl-relativetimeformat-prototype-format.js: -------------------------------------------------------------------------------- 1 | var rtf1 = new Intl.RelativeTimeFormat('en', { style: 'narrow' }); 2 | 3 | console.log(rtf1.format(3, 'quarter')); 4 | // expected output: "in 3 qtrs." 5 | 6 | console.log(rtf1.format(-1, 'day')); 7 | // expected output: "1 day ago" 8 | 9 | console.log(rtf1.format(10, 'seconds')); 10 | // expected output: "in 10 sec." -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-hasownproperty.js: -------------------------------------------------------------------------------- 1 | const object1 = new Object(); 2 | object1.property1 = 42; 3 | 4 | console.log(object1.hasOwnProperty('property1')); 5 | // expected output: true 6 | 7 | console.log(object1.hasOwnProperty('toString')); 8 | // expected output: false 9 | 10 | console.log(object1.hasOwnProperty('hasOwnProperty')); 11 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-exec.js: -------------------------------------------------------------------------------- 1 | var regex1 = RegExp('foo*','g'); 2 | var str1 = 'table football, foosball'; 3 | var array1; 4 | 5 | while ((array1 = regex1.exec(str1)) !== null) { 6 | console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`); 7 | // expected output: "Found foo. Next starts at 9." 8 | // expected output: "Found foo. Next starts at 19." 9 | } -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-exchange.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 5; 5 | 6 | console.log(Atomics.load(uint8, 0)); 7 | // expected output: 5 8 | 9 | Atomics.exchange(uint8, 0, 2); // returns 5 10 | console.log(Atomics.load(uint8, 0)); 11 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/date/date-now.js: -------------------------------------------------------------------------------- 1 | // this example takes 2 seconds to run 2 | const start = Date.now(); 3 | 4 | console.log('starting timer...'); 5 | // expected output: starting timer... 6 | 7 | setTimeout(() => { 8 | const millis = Date.now() - start; 9 | 10 | console.log(`seconds elapsed = ${Math.floor(millis/1000)}`); 11 | // expected output : seconds elapsed = 2 12 | }, 2000); -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-parsefloat.js: -------------------------------------------------------------------------------- 1 | function circumference(r) { 2 | return parseFloat(r) * 2.0 * Math.PI; 3 | } 4 | 5 | console.log(circumference(4.567)); 6 | // expected output: 28.695307297889173 7 | 8 | console.log(circumference('4.567abcdefgh')); 9 | // expected output: 28.695307297889173 10 | 11 | console.log(circumference('abcdefgh')); 12 | // expected output: NaN -------------------------------------------------------------------------------- /JavaScript/intl/intl-collator-supportedlocalesof.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var locales1 = ['ban', 'id-u-co-pinyin', 'de-ID']; 4 | var options1 = { localeMatcher: 'lookup' }; 5 | 6 | console.log(Intl.Collator.supportedLocalesOf(locales1, options1)); 7 | // expected output: Array ["id-u-co-pinyin", "de-ID"] 8 | // (Note: the exact output may be browser-dependent) -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-keyfor.js: -------------------------------------------------------------------------------- 1 | const globalSym = Symbol.for('foo'); // global symbol 2 | 3 | console.log(Symbol.keyFor(globalSym)); 4 | // expected output: "foo" 5 | 6 | const localSym = Symbol(); // local symbol 7 | 8 | console.log(Symbol.keyFor(localSym)); 9 | // expected output: undefined 10 | 11 | console.log(Symbol.keyFor(Symbol.iterator)); 12 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-prototype-description.js: -------------------------------------------------------------------------------- 1 | console.log(Symbol('desc').description); 2 | // expected output: "desc" 3 | 4 | console.log(Symbol.iterator.description); 5 | // expected output: "Symbol.iterator" 6 | 7 | console.log(Symbol.for('foo').description); 8 | // expected output: "foo" 9 | 10 | console.log(Symbol('foo').description + 'bar'); 11 | // expected output: "foobar" -------------------------------------------------------------------------------- /JavaScript/arraybuffer/arraybuffer-slice.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | const int32View = new Int32Array(buffer); 4 | // produces Int32Array [0, 0, 0, 0] 5 | 6 | int32View[1] = 42; 7 | const sliced = new Int32Array(buffer.slice(4,12)); 8 | // produces Int32Array [42, 0] 9 | 10 | console.log(sliced[0]); 11 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/bigint/bigint-tolocalestring.js: -------------------------------------------------------------------------------- 1 | const bigint = 123456789123456789n; 2 | 3 | // German uses period for thousands 4 | console.log(bigint.toLocaleString('de-DE')); 5 | // expected output: "123.456.789.123.456.789" 6 | 7 | // request a currency format 8 | console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' })); 9 | // expected output: "€123.456.789.123.456.789,00" -------------------------------------------------------------------------------- /JavaScript/date/date-setutchours.js: -------------------------------------------------------------------------------- 1 | const event = new Date('August 19, 1975 23:15:30 GMT-3:00'); 2 | 3 | console.log(event.toUTCString()); 4 | // expected output: Wed, 20 Aug 1975 02:15:30 GMT 5 | 6 | console.log(event.getUTCHours()); 7 | // expected output: 2 8 | 9 | event.setUTCHours(23); 10 | 11 | console.log(event.toUTCString()); 12 | // expected output: Wed, 20 Aug 1975 23:15:30 GMT -------------------------------------------------------------------------------- /JavaScript/intl/intl-relativetimeformat-prototype-formattoparts.js: -------------------------------------------------------------------------------- 1 | var rtf1 = new Intl.RelativeTimeFormat('en', { numeric: 'auto' }); 2 | var parts = rtf1.formatToParts(10, 'seconds'); 3 | 4 | console.log(parts[0].value); 5 | // expected output: "in " 6 | 7 | console.log(parts[1].value); 8 | // expected output: "10" 9 | 10 | console.log(parts[2].value); 11 | // expected output: " seconds" -------------------------------------------------------------------------------- /JavaScript/number/number-max-value.js: -------------------------------------------------------------------------------- 1 | function multiply(x, y) { 2 | if (x * y > Number.MAX_VALUE) { 3 | return ("Process as Infinity"); 4 | } 5 | return (x * y); 6 | } 7 | 8 | console.log(multiply(1.7976931348623157e+308, 1)); 9 | // expected output: 1.7976931348623157e+308 10 | 11 | console.log(multiply(1.7976931348623157e+308, 2)); 12 | // expected output: "Process as Infinity" -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-isprototypeof.js: -------------------------------------------------------------------------------- 1 | function object1() {} 2 | function object2() {} 3 | 4 | object1.prototype = Object.create(object2.prototype); 5 | 6 | const object3 = new object1(); 7 | 8 | console.log(object1.prototype.isPrototypeOf(object3)); 9 | // expected output: true 10 | 11 | console.log(object2.prototype.isPrototypeOf(object3)); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/promise/promise-allsettled.js: -------------------------------------------------------------------------------- 1 | const promise1 = Promise.resolve(3); 2 | const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo')); 3 | const promises = [promise1, promise2]; 4 | 5 | Promise.allSettled(promises). 6 | then((results) => results.forEach((result) => console.log(result.status))); 7 | 8 | // expected output: 9 | // "fulfilled" 10 | // "rejected" -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-setprototypeof.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | console.log(Reflect.setPrototypeOf(object1, Object.prototype)); 4 | // expected output: true 5 | 6 | console.log(Reflect.setPrototypeOf(object1, null)); 7 | // expected output: true 8 | 9 | const object2 = {}; 10 | 11 | console.log(Reflect.setPrototypeOf(Object.freeze(object2), null)); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-@@match.js: -------------------------------------------------------------------------------- 1 | class RegExp1 extends RegExp { 2 | [Symbol.match](str) { 3 | var result = RegExp.prototype[Symbol.match].call(this, str); 4 | if (result) { 5 | return 'VALID'; 6 | } 7 | return 'INVALID'; 8 | } 9 | } 10 | 11 | console.log('2012-07-02'.match(new RegExp1('([0-9]+)-([0-9]+)-([0-9]+)'))); 12 | // expected output: "VALID" -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-unicode.js: -------------------------------------------------------------------------------- 1 | var regex1 = new RegExp('\u{61}'); 2 | var regex2 = new RegExp('\u{61}', 'u'); 3 | 4 | console.log(regex1.unicode); 5 | // expected output: false 6 | 7 | console.log(regex2.unicode); 8 | // expected output: true 9 | 10 | console.log(regex1.source); 11 | // expected output: "a" 12 | 13 | console.log(regex2.source); 14 | // expected output: "a" -------------------------------------------------------------------------------- /JavaScript/statement/statement-default.js: -------------------------------------------------------------------------------- 1 | var expr = 'Pears'; 2 | switch(expr) { 3 | case 'Oranges': 4 | console.log('Oranges are $0.59 a pound.'); 5 | break; 6 | case 'Apples': 7 | console.log('Apples are $0.32 a pound.'); 8 | break; 9 | default: 10 | console.log('Sorry, we are out of ' + expr + '.'); 11 | // expected output: "Sorry, we are out of Pears." 12 | } -------------------------------------------------------------------------------- /JavaScript/string/string-search.js: -------------------------------------------------------------------------------- 1 | var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; 2 | 3 | // any character that is not a word character or whitespace 4 | var regex = /[^\w\s]/g; 5 | 6 | console.log(paragraph.search(regex)); 7 | // expected output: 43 8 | 9 | console.log(paragraph[paragraph.search(regex)]); 10 | // expected output: "." -------------------------------------------------------------------------------- /JavaScript/string/string-slice.js: -------------------------------------------------------------------------------- 1 | var str = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | console.log(str.slice(31)); 4 | // expected output: "the lazy dog." 5 | 6 | console.log(str.slice(4, 19)); 7 | // expected output: "quick brown fox" 8 | 9 | console.log(str.slice(-4)); 10 | // expected output: "dog." 11 | 12 | console.log(str.slice(-9, -5)); 13 | // expected output: "lazy" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-some.js: -------------------------------------------------------------------------------- 1 | function isNegative(element, index, array) { 2 | return element < 0; 3 | } 4 | 5 | const int8 = new Int8Array([-10, 20, -30, 40, -50]); 6 | const positives = new Int8Array([10, 20, 30, 40, 50]); 7 | 8 | console.log(int8.some(isNegative)); 9 | // expected output: true 10 | 11 | console.log(positives.some(isNegative)); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-slice.js: -------------------------------------------------------------------------------- 1 | const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; 2 | 3 | console.log(animals.slice(2)); 4 | // expected output: Array ["camel", "duck", "elephant"] 5 | 6 | console.log(animals.slice(2, 4)); 7 | // expected output: Array ["camel", "duck"] 8 | 9 | console.log(animals.slice(1, 5)); 10 | // expected output: Array ["bison", "camel", "duck", "elephant"] -------------------------------------------------------------------------------- /JavaScript/function/function-bind.js: -------------------------------------------------------------------------------- 1 | const module = { 2 | x: 42, 3 | getX: function() { 4 | return this.x; 5 | } 6 | } 7 | 8 | const unboundGetX = module.getX; 9 | console.log(unboundGetX()); // The function gets invoked at the global scope 10 | // expected output: undefined 11 | 12 | const boundGetX = unboundGetX.bind(module); 13 | console.log(boundGetX()); 14 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/intl/intl-numberformat-supportedlocalesof.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var locales1 = ['ban', 'id-u-co-pinyin', 'de-ID']; 4 | var options1 = { localeMatcher: 'lookup' }; 5 | 6 | console.log(Intl.NumberFormat.supportedLocalesOf(locales1, options1)); 7 | // expected output: Array ["id-u-co-pinyin", "de-ID"] 8 | // (Note: the exact output may be browser-dependent) -------------------------------------------------------------------------------- /JavaScript/array/array-pop.js: -------------------------------------------------------------------------------- 1 | const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; 2 | 3 | console.log(plants.pop()); 4 | // expected output: "tomato" 5 | 6 | console.log(plants); 7 | // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"] 8 | 9 | plants.pop(); 10 | 11 | console.log(plants); 12 | // expected output: Array ["broccoli", "cauliflower", "cabbage"] -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-constructor.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | // Create a couple of views 5 | const view1 = new DataView(buffer); 6 | const view2 = new DataView(buffer,12,4); //from byte 12 for the next 4 bytes 7 | view1.setInt8(12, 42); // put 42 in slot 12 8 | 9 | console.log(view2.getInt8(0)); 10 | // expected output: 42 -------------------------------------------------------------------------------- /JavaScript/intl/intl-datetimeformat-supportedlocalesof.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var locales1 = ['ban', 'id-u-co-pinyin', 'de-ID']; 4 | var options1 = { localeMatcher: 'lookup' }; 5 | 6 | console.log(Intl.DateTimeFormat.supportedLocalesOf(locales1, options1)); 7 | // expected output: Array ["id-u-co-pinyin", "de-ID"] 8 | // (Note: the exact output may be browser-dependent) -------------------------------------------------------------------------------- /JavaScript/intl/intl-locale-prototype-tostring.js: -------------------------------------------------------------------------------- 1 | const french = new Intl.Locale("fr-Latn-FR", {calendar: "gregory", hourCycle: "h24"}); 2 | const korean = new Intl.Locale("ko-Kore-KR", {numeric: true, caseFirst: "upper"}); 3 | 4 | console.log(french.toString()); 5 | // expected output: "fr-Latn-FR-u-ca-gregory-hc-h24" 6 | 7 | console.log(korean.toString()) 8 | //expected output: "ko-Kore-KR-u-kf-upper-kn" -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-isextensible.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | 3 | console.log(Reflect.isExtensible(object1)); 4 | // expected output: true 5 | 6 | Reflect.preventExtensions(object1); 7 | 8 | console.log(Reflect.isExtensible(object1)); 9 | // expected output: false 10 | 11 | const object2 = Object.seal({}); 12 | 13 | console.log(Reflect.isExtensible(object2)); 14 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/array/array-splice.js: -------------------------------------------------------------------------------- 1 | const months = ['Jan', 'March', 'April', 'June']; 2 | months.splice(1, 0, 'Feb'); 3 | // inserts at index 1 4 | console.log(months); 5 | // expected output: Array ["Jan", "Feb", "March", "April", "June"] 6 | 7 | months.splice(4, 1, 'May'); 8 | // replaces 1 element at index 4 9 | console.log(months); 10 | // expected output: Array ["Jan", "Feb", "March", "April", "May"] -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getbigint64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | // Highest possible BigInt value that fits in a signed 64-bit integer 5 | const max = 2n ** (64n -1n) - 1n; 6 | 7 | const view = new DataView(buffer); 8 | view.setBigInt64(1, max); 9 | 10 | console.log(view.getBigInt64(1)); 11 | // expected output: 9223372036854775807 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-getbiguint64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | // Highest possible BigInt value that fits in an unsigned 64-bit integer 5 | const max = 2n ** 64n - 1n; 6 | 7 | const view = new DataView(buffer); 8 | view.setBigUint64(1, max); 9 | 10 | console.log(view.getBigUint64(1)); 11 | // expected output: 18446744073709551615 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setbigint64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | // Highest possible BigInt value that fits in a signed 64-bit integer 5 | const max = 2n ** (64n -1n) - 1n; 6 | 7 | const view = new DataView(buffer); 8 | view.setBigInt64(1, max); 9 | 10 | console.log(view.getBigInt64(1)); 11 | // expected output: 9223372036854775807 -------------------------------------------------------------------------------- /JavaScript/dataview/dataview-setbiguint64.js: -------------------------------------------------------------------------------- 1 | // create an ArrayBuffer with a size in bytes 2 | const buffer = new ArrayBuffer(16); 3 | 4 | // Highest possible BigInt value that fits in an unsigned 64-bit integer 5 | const max = 2n ** 64n - 1n; 6 | 7 | const view = new DataView(buffer); 8 | view.setBigUint64(1, max); 9 | 10 | console.log(view.getBigUint64(1)); 11 | // expected output: 18446744073709551615 -------------------------------------------------------------------------------- /JavaScript/date/date-setutcfullyear.js: -------------------------------------------------------------------------------- 1 | const event = new Date('December 31, 1975 23:15:30 GMT-3:00'); 2 | 3 | console.log(event.getUTCFullYear()); 4 | // expected output: 1976 5 | 6 | console.log(event.toUTCString()); 7 | // expected output: Thu, 01 Jan 1976 02:15:30 GMT 8 | 9 | event.setUTCFullYear(1975); 10 | 11 | console.log(event.toUTCString()); 12 | // expected output: Wed, 01 Jan 1975 02:15:30 GMT -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-decodeuricomponent.js: -------------------------------------------------------------------------------- 1 | function containsEncodedComponents(x) { 2 | // ie ?,=,&,/ etc 3 | return (decodeURI(x) !== decodeURIComponent(x)); 4 | } 5 | 6 | console.log(containsEncodedComponents('%3Fx%3Dtest')); // ?x=test 7 | // expected output: true 8 | 9 | console.log(containsEncodedComponents('%D1%88%D0%B5%D0%BB%D0%BB%D1%8B')); // шеллы 10 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/intl/intl-datetimeformat-prototype-resolvedoptions.js: -------------------------------------------------------------------------------- 1 | var region1 = new Intl.DateTimeFormat('zh-CN', { timeZone: 'UTC' }); 2 | var options1 = region1.resolvedOptions(); 3 | 4 | console.log(options1.locale); 5 | // expected output: "zh-CN" 6 | 7 | console.log(options1.calendar); 8 | // expected output: "gregory" 9 | 10 | console.log(options1.numberingSystem); 11 | // expected output: "latn" -------------------------------------------------------------------------------- /JavaScript/intl/intl-relativetimeformat-supportedlocalesof.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var locales1 = ['ban', 'id-u-co-pinyin', 'de-ID']; 4 | var options1 = { localeMatcher: 'lookup' }; 5 | 6 | console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales1, options1)); 7 | // expected output: Array ["id-u-co-pinyin", "de-ID"] 8 | // (Note: the exact output may be browser-dependent) -------------------------------------------------------------------------------- /JavaScript/math/math-e.js: -------------------------------------------------------------------------------- 1 | function compoundOneYear(interestRate, currentVal) { 2 | return currentVal * (Math.E ** interestRate); 3 | } 4 | 5 | console.log(Math.E); 6 | // expected output: 2.718281828459045 7 | 8 | console.log((1 + (1/1000000)) ** 1000000); 9 | // expected output: 2.718280469 (approximately) 10 | 11 | console.log(compoundOneYear(0.05, 100)); 12 | // expected output: 105.12710963760242 -------------------------------------------------------------------------------- /JavaScript/intl/intl-collator.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | function letterSort(lang, letters) { 4 | letters.sort(new Intl.Collator(lang).compare); 5 | return letters; 6 | } 7 | 8 | console.log(letterSort('de', ['a','z','ä'])); 9 | // expected output: Array ["a", "ä", "z"] 10 | 11 | console.log(letterSort('sv', ['a','z','ä'])); 12 | // expected output: Array ["a", "z", "ä"] -------------------------------------------------------------------------------- /JavaScript/sharedarraybuffer/sharedarraybuffer-slice.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const int32View = new Int32Array(buffer); // create the view 4 | // produces Int32Array [0, 0, 0, 0] 5 | 6 | int32View[1] = 42; 7 | const sliced = new Int32Array(buffer.slice(4,12)); 8 | 9 | console.log(sliced); 10 | // expected output: Int32Array [42, 0] -------------------------------------------------------------------------------- /JavaScript/string/string-iterator.js: -------------------------------------------------------------------------------- 1 | const str = 'The quick red fox jumped over the lazy dog\'s back.'; 2 | 3 | let iterator = str[Symbol.iterator](); 4 | let theChar = iterator.next(); 5 | 6 | while(!theChar.done && theChar.value !== ' ') { 7 | console.log(theChar.value); 8 | theChar = iterator.next(); 9 | // expected output: "T" 10 | // "h" 11 | // "e" 12 | } -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-split.js: -------------------------------------------------------------------------------- 1 | class Split1 { 2 | constructor(value) { 3 | this.value = value; 4 | } 5 | [Symbol.split](string) { 6 | var index = string.indexOf(this.value); 7 | return this.value + string.substr(0, index) + "/" 8 | + string.substr(index + this.value.length); 9 | } 10 | } 11 | 12 | console.log('foobar'.split(new Split1('foo'))); 13 | // expected output: "foo/bar" -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-decodeuri.js: -------------------------------------------------------------------------------- 1 | const uri = 'https://mozilla.org/?x=шеллы'; 2 | const encoded = encodeURI(uri); 3 | console.log(encoded); 4 | // expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" 5 | 6 | try { 7 | console.log(decodeURI(encoded)); 8 | // expected output: "https://mozilla.org/?x=шеллы" 9 | } catch(e) { // catches a malformed URI 10 | console.error(e); 11 | } -------------------------------------------------------------------------------- /JavaScript/globalprops/globalprops-encodeuri.js: -------------------------------------------------------------------------------- 1 | const uri = 'https://mozilla.org/?x=шеллы'; 2 | const encoded = encodeURI(uri); 3 | console.log(encoded); 4 | // expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" 5 | 6 | try { 7 | console.log(decodeURI(encoded)); 8 | // expected output: "https://mozilla.org/?x=шеллы" 9 | } catch(e) { // catches a malformed URI 10 | console.error(e); 11 | } -------------------------------------------------------------------------------- /JavaScript/intl/intl-getcanonicallocales.js: -------------------------------------------------------------------------------- 1 | console.log(Intl.getCanonicalLocales('EN-US')); 2 | // expected output: Array ["en-US"] 3 | 4 | console.log(Intl.getCanonicalLocales(['EN-US', 'Fr'])); 5 | // expected output: Array ["en-US", "fr"] 6 | 7 | try { 8 | Intl.getCanonicalLocales('EN_US'); 9 | } catch (err) { 10 | console.log(err); 11 | // expected output: RangeError: invalid language tag: EN_US 12 | } -------------------------------------------------------------------------------- /JavaScript/statement/statement-async.js: -------------------------------------------------------------------------------- 1 | function resolveAfter2Seconds() { 2 | return new Promise(resolve => { 3 | setTimeout(() => { 4 | resolve('resolved'); 5 | }, 2000); 6 | }); 7 | } 8 | 9 | async function asyncCall() { 10 | console.log('calling'); 11 | var result = await resolveAfter2Seconds(); 12 | console.log(result); 13 | // expected output: 'resolved' 14 | } 15 | 16 | asyncCall(); -------------------------------------------------------------------------------- /JavaScript/object/object-prototype-propertyisenumerable.js: -------------------------------------------------------------------------------- 1 | const object1 = {}; 2 | const array1 = []; 3 | object1.property1 = 42; 4 | array1[0] = 42; 5 | 6 | console.log(object1.propertyIsEnumerable('property1')); 7 | // expected output: true 8 | 9 | console.log(array1.propertyIsEnumerable(0)); 10 | // expected output: true 11 | 12 | console.log(array1.propertyIsEnumerable('length')); 13 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/string/string-split.js: -------------------------------------------------------------------------------- 1 | var str = 'The quick brown fox jumps over the lazy dog.'; 2 | 3 | var words = str.split(' '); 4 | console.log(words[3]); 5 | // expected output: "fox" 6 | 7 | var chars = str.split(''); 8 | console.log(chars[8]); 9 | // expected output: "k" 10 | 11 | var strCopy = str.split(); 12 | console.log(strCopy); 13 | // expected output: Array ["The quick brown fox jumps over the lazy dog."] -------------------------------------------------------------------------------- /JavaScript/bigint/bigint-asintn.js: -------------------------------------------------------------------------------- 1 | const max = 2n ** (64n - 1n) - 1n; 2 | 3 | function check64bit(number) { 4 | (number > max) ? 5 | console.log("Number doesn't fit in signed 64-bit integer!") : 6 | console.log(BigInt.asIntN(64, number)); 7 | } 8 | 9 | check64bit(2n ** 64n); 10 | // expected output: "Number doesn't fit in signed 64-bit integer!" 11 | 12 | check64bit(2n ** 32n); 13 | // expected output: 4294967296 -------------------------------------------------------------------------------- /JavaScript/bigint/bigint-asuintn.js: -------------------------------------------------------------------------------- 1 | const max = 2n ** 64n - 1n; 2 | 3 | function check64bit(number) { 4 | (number > max) ? 5 | console.log("Number doesn't fit in unsigned 64-bit integer!") : 6 | console.log(BigInt.asUintN(64, number)); 7 | } 8 | 9 | check64bit(2n ** 64n); 10 | // expected output: "Number doesn't fit in unsigned 64-bit integer!" 11 | 12 | check64bit(2n ** 32n); 13 | // expected output: 4294967296 -------------------------------------------------------------------------------- /JavaScript/number/number-positive-infinity.js: -------------------------------------------------------------------------------- 1 | function checkNumber(bigNumber) { 2 | if (bigNumber === Number.POSITIVE_INFINITY) { 3 | return 'Process number as Infinity'; 4 | } 5 | return bigNumber; 6 | } 7 | 8 | console.log(checkNumber(Number.MAX_VALUE)); 9 | // expected output: 1.7976931348623157e+308 10 | 11 | console.log(checkNumber(Number.MAX_VALUE * 2)); 12 | // expected output: Process number as Infinity -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-prototype-tostring.js: -------------------------------------------------------------------------------- 1 | console.log(Symbol('desc').toString()); 2 | // expected output: "Symbol(desc)" 3 | 4 | console.log(Symbol.iterator.toString()); 5 | // expected output: "Symbol(Symbol.iterator) 6 | 7 | console.log(Symbol.for('foo').toString()); 8 | // expected output: "Symbol(foo)" 9 | 10 | // console.log(Symbol('foo') + 'bar'); 11 | // expected output: Error: Can't convert symbol to string -------------------------------------------------------------------------------- /JavaScript/array/array-push.js: -------------------------------------------------------------------------------- 1 | const animals = ['pigs', 'goats', 'sheep']; 2 | 3 | const count = animals.push('cows'); 4 | console.log(count); 5 | // expected output: 4 6 | console.log(animals); 7 | // expected output: Array ["pigs", "goats", "sheep", "cows"] 8 | 9 | animals.push('chickens', 'cats', 'dogs'); 10 | console.log(animals); 11 | // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"] -------------------------------------------------------------------------------- /JavaScript/classes/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "classes-constructor.js", 4 | "title": "JavaScript: Classes Constructor", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "classes-extends.js", 9 | "title": "JavaScript: Classes Extends", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "classes-static.js", 14 | "title": "JavaScript: Classes Static", 15 | "type": "js" 16 | } 17 | ] -------------------------------------------------------------------------------- /JavaScript/date/date-tolocaletimestring.js: -------------------------------------------------------------------------------- 1 | // Depending on timezone, your results will vary 2 | const event = new Date('August 19, 1975 23:15:30 GMT+00:00'); 3 | 4 | console.log(event.toLocaleTimeString('en-US')); 5 | // expected output: 1:15:30 AM 6 | 7 | console.log(event.toLocaleTimeString('it-IT')); 8 | // expected output: 01:15:30 9 | 10 | console.log(event.toLocaleTimeString('ar-EG')); 11 | // expected output: ١٢:١٥:٣٠ ص -------------------------------------------------------------------------------- /JavaScript/intl/intl-locale.js: -------------------------------------------------------------------------------- 1 | const korean = new Intl.Locale('ko', { 2 | script: 'Kore', region: 'KR', hourCycle: 'h24', calendar: 'gregory' 3 | }); 4 | 5 | const japanese = new Intl.Locale('ja-Jpan-JP-u-ca-japanese-hc-h12'); 6 | 7 | console.log(korean.baseName, japanese.baseName); 8 | // expected output: "ko-Kore-KR" "ja-Jpan-JP" 9 | 10 | console.log(korean.hourCycle, japanese.hourCycle); 11 | // expected output: "h24" "h12" -------------------------------------------------------------------------------- /JavaScript/intl/intl-numberformat-prototype-resolvedoptions.js: -------------------------------------------------------------------------------- 1 | var numberFormat1 = new Intl.NumberFormat('de-DE'); 2 | var options1 = numberFormat1.resolvedOptions(); 3 | 4 | console.log(options1.locale); 5 | // expected output (Firefox / Safari): "de-DE" 6 | // expected output (Chrome): "de" 7 | 8 | console.log(options1.numberingSystem); 9 | // expected output: "latn" 10 | 11 | console.log(options1.style); 12 | // "decimal" -------------------------------------------------------------------------------- /JavaScript/promise/promise-race.js: -------------------------------------------------------------------------------- 1 | var promise1 = new Promise(function(resolve, reject) { 2 | setTimeout(resolve, 500, 'one'); 3 | }); 4 | 5 | var promise2 = new Promise(function(resolve, reject) { 6 | setTimeout(resolve, 100, 'two'); 7 | }); 8 | 9 | Promise.race([promise1, promise2]).then(function(value) { 10 | console.log(value); 11 | // Both resolve, but promise2 is faster 12 | }); 13 | // expected output: "two" -------------------------------------------------------------------------------- /JavaScript/typedarray/typedarray-constructor.js: -------------------------------------------------------------------------------- 1 | // create a TypedArray with a size in bytes 2 | const typedArray1 = new Int8Array(8); 3 | typedArray1[0] = 32; 4 | 5 | const typedArray2 = new Int8Array(typedArray1); 6 | typedArray2[1] = 42; 7 | 8 | console.log(typedArray1); 9 | // expected output: Int8Array [32, 0, 0, 0, 0, 0, 0, 0] 10 | 11 | console.log(typedArray2); 12 | // expected output: Int8Array [32, 42, 0, 0, 0, 0, 0, 0] -------------------------------------------------------------------------------- /JavaScript/intl/intl-locale-prototype-maximize.js: -------------------------------------------------------------------------------- 1 | const english = new Intl.Locale("en"); 2 | const korean = new Intl.Locale("ko"); 3 | const arabic = new Intl.Locale("ar"); 4 | 5 | console.log(english.maximize().baseName); 6 | // expected output: "en-Latn-US" 7 | 8 | console.log(korean.maximize().baseName); 9 | // expected output: "ko-Kore-KR" 10 | 11 | console.log(arabic.maximize().baseName); 12 | // expected output: "ar-Arab-EG" -------------------------------------------------------------------------------- /JavaScript/intl/intl-locale-prototype-minimize.js: -------------------------------------------------------------------------------- 1 | const english = new Intl.Locale("en-Latn-US"); 2 | const korean = new Intl.Locale("ko-Kore-KR"); 3 | const arabic = new Intl.Locale("ar-Arab-EG"); 4 | 5 | console.log(english.minimize().baseName); 6 | // expected output: "en" 7 | 8 | console.log(korean.minimize().baseName); 9 | // expected output: "ko" 10 | 11 | console.log(arabic.minimize().baseName); 12 | // expected output: "ar" -------------------------------------------------------------------------------- /JavaScript/intl/intl-relativetimeformat.js: -------------------------------------------------------------------------------- 1 | var rtf1 = new Intl.RelativeTimeFormat('en', { style: 'narrow' }); 2 | 3 | console.log(rtf1.format(3, 'quarter')); 4 | //expected output: "in 3 qtrs." 5 | 6 | console.log(rtf1.format(-1, 'day')); 7 | //expected output: "1 day ago" 8 | 9 | var rtf2 = new Intl.RelativeTimeFormat('es', { numeric: 'auto' }); 10 | 11 | console.log(rtf2.format(2, 'day')); 12 | //expected output: "pasado mañana" -------------------------------------------------------------------------------- /JavaScript/number/number-negative-infinity.js: -------------------------------------------------------------------------------- 1 | function checkNumber(smallNumber) { 2 | if (smallNumber === Number.NEGATIVE_INFINITY) { 3 | return 'Process number as -Infinity'; 4 | } 5 | return smallNumber; 6 | } 7 | 8 | console.log(checkNumber(-Number.MAX_VALUE)); 9 | // expected output: -1.7976931348623157e+308 10 | 11 | console.log(checkNumber(-Number.MAX_VALUE * 2)); 12 | // expected output: "Process number as -Infinity" -------------------------------------------------------------------------------- /JavaScript/boolean/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "boolean-constructor.js", 4 | "title": "JavaScript: Boolean Constructor", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "boolean-tostring.js", 9 | "title": "JavaScript: Boolean.toString()", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "boolean-valueof.js", 14 | "title": "JavaScript: Boolean.valueOf()", 15 | "type": "js" 16 | } 17 | ] -------------------------------------------------------------------------------- /JavaScript/date/date-gettimezoneoffset.js: -------------------------------------------------------------------------------- 1 | const date1 = new Date('August 19, 1975 23:15:30 GMT+07:00'); 2 | const date2 = new Date('August 19, 1975 23:15:30 GMT-02:00'); 3 | 4 | console.log(date1.getTimezoneOffset()); 5 | // expected output: your local timezone offset in minutes 6 | // (eg -120). NOT the timezone offset of the date object. 7 | 8 | console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset()); 9 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-getownpropertydescriptor.js: -------------------------------------------------------------------------------- 1 | const object1 = { 2 | property1: 42 3 | }; 4 | 5 | console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').value); 6 | // expected output: 42 7 | 8 | console.log(Reflect.getOwnPropertyDescriptor(object1, 'property2')); 9 | // expected output: undefined 10 | 11 | console.log(Reflect.getOwnPropertyDescriptor(object1, 'property1').writable); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-multiline.js: -------------------------------------------------------------------------------- 1 | var regex1 = new RegExp('^football'); 2 | var regex2 = new RegExp('^football', 'm'); 3 | 4 | console.log(regex1.multiline); 5 | // expected output: false 6 | 7 | console.log(regex2.multiline); 8 | // expected output: true 9 | 10 | console.log(regex1.test('rugby\nfootball')); 11 | // expected output: false 12 | 13 | console.log(regex2.test('rugby\nfootball')); 14 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/statement/statement-switch.js: -------------------------------------------------------------------------------- 1 | var expr = 'Papayas'; 2 | switch (expr) { 3 | case 'Oranges': 4 | console.log('Oranges are $0.59 a pound.'); 5 | break; 6 | case 'Mangoes': 7 | case 'Papayas': 8 | console.log('Mangoes and papayas are $2.79 a pound.'); 9 | // expected output: "Mangoes and papayas are $2.79 a pound." 10 | break; 11 | default: 12 | console.log('Sorry, we are out of ' + expr + '.'); 13 | } -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-isconcatspreadable.js: -------------------------------------------------------------------------------- 1 | const alpha = ['a', 'b', 'c']; 2 | const numeric = [1, 2, 3]; 3 | let alphaNumeric = alpha.concat(numeric); 4 | 5 | console.log(alphaNumeric); 6 | // expected output: Array ["a", "b", "c", 1, 2, 3] 7 | 8 | numeric[Symbol.isConcatSpreadable] = false; 9 | alphaNumeric = alpha.concat(numeric); 10 | 11 | console.log(alphaNumeric); 12 | // expected output: Array ["a", "b", "c", Array [1, 2, 3]] -------------------------------------------------------------------------------- /JavaScript/intl/intl-collator-prototype-compare.js: -------------------------------------------------------------------------------- 1 | const enCollator = new Intl.Collator('en'); 2 | const deCollator = new Intl.Collator('de'); 3 | const svCollator = new Intl.Collator('sv'); 4 | 5 | console.log(enCollator.compare('z', 'a') > 0); 6 | // expected output: true 7 | 8 | console.log(deCollator.compare('z', 'ä') > 0); 9 | // expected output: true 10 | 11 | console.log(svCollator.compare('z', 'ä') > 0); 12 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/reflect/reflect-apply.js: -------------------------------------------------------------------------------- 1 | console.log(Reflect.apply(Math.floor, undefined, [1.75])); 2 | // expected output: 1 3 | 4 | console.log(Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111])); 5 | // expected output: "hello" 6 | 7 | console.log(Reflect.apply(RegExp.prototype.exec, /ab/, ['confabulation']).index); 8 | // expected output: 4 9 | 10 | console.log(Reflect.apply(''.charAt, 'ponies', [3])); 11 | // expected output: "i" -------------------------------------------------------------------------------- /JavaScript/atomics/atomics-compareexchange.js: -------------------------------------------------------------------------------- 1 | // create a SharedArrayBuffer with a size in bytes 2 | const buffer = new SharedArrayBuffer(16); 3 | const uint8 = new Uint8Array(buffer); 4 | uint8[0] = 5; 5 | 6 | Atomics.compareExchange(uint8, 0, 5, 2); // returns 5 7 | console.log(Atomics.load(uint8, 0)); 8 | // expected output: 2 9 | 10 | Atomics.compareExchange(uint8, 0, 5, 4); // returns 2 11 | console.log(Atomics.load(uint8, 0)); 12 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-ownkeys.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | _age: 111, 3 | [Symbol('secret')]: 'I am scared!', 4 | eyeCount: 4 5 | } 6 | 7 | const handler1 = { 8 | ownKeys (target) { 9 | return Reflect.ownKeys(target) 10 | } 11 | } 12 | 13 | const proxy1 = new Proxy(monster1, handler1); 14 | 15 | for (let key of Object.keys(proxy1)) { 16 | console.log(key); 17 | // expected output: "_age" 18 | // expected output: "eyeCount" 19 | } -------------------------------------------------------------------------------- /JavaScript/weakset/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "weakset-prototype-add.js", 4 | "title": "JavaScript: WeakSet.Prototype.add()", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "weakset-prototype-delete.js", 9 | "title": "JavaScript: WeakSet.Prototype.delete()", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "weakset-prototype-has.js", 14 | "title": "JavaScript: WeakSet.Prototype.has()", 15 | "type": "js" 16 | } 17 | ] -------------------------------------------------------------------------------- /JavaScript/weakset/weakset-prototype-add.js: -------------------------------------------------------------------------------- 1 | const weakset1 = new WeakSet(); 2 | const object1 = {}; 3 | 4 | weakset1.add(object1); 5 | console.log(weakset1.has(object1)); 6 | // expected output: true 7 | 8 | try { 9 | weakset1.add(1); 10 | } catch(error) { 11 | console.log(error); 12 | // expected output: "Error: Invalid value used in weak set" in Chrome 13 | // expected output: "TypeError: WeakSet value must be an object, got the number 1" in Firefox 14 | } -------------------------------------------------------------------------------- /JavaScript/date/date-tolocalestring.js: -------------------------------------------------------------------------------- 1 | const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); 2 | 3 | // British English uses day-month-year order and 24-hour time without AM/PM 4 | console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' })); 5 | // expected output: 20/12/2012, 03:00:00 6 | 7 | // Korean uses year-month-day order and 12-hour time with AM/PM 8 | console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' })); 9 | // expected output: 2012. 12. 20. 오전 3:00:00 -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-@@split.js: -------------------------------------------------------------------------------- 1 | class RegExp1 extends RegExp { 2 | [Symbol.split](str, limit) { 3 | var result = RegExp.prototype[Symbol.split].call(this, str, limit); 4 | return result.map(x => "(" + x + ")"); 5 | } 6 | } 7 | 8 | console.log('2016-01-02'.split(new RegExp1('-'))); 9 | // expected output: Array ["(2016)", "(01)", "(02)"] 10 | 11 | console.log('2016-01-02'.split(new RegExp('-'))); 12 | // expected output: Array ["2016", "01", "02"] -------------------------------------------------------------------------------- /JavaScript/string/string-replace.js: -------------------------------------------------------------------------------- 1 | var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?'; 2 | 3 | var regex = /dog/gi; 4 | 5 | console.log(p.replace(regex, 'ferret')); 6 | // expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?" 7 | 8 | console.log(p.replace('dog', 'monkey')); 9 | // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?" -------------------------------------------------------------------------------- /JavaScript/object/object-create.js: -------------------------------------------------------------------------------- 1 | const person = { 2 | isHuman: false, 3 | printIntroduction: function () { 4 | console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`); 5 | } 6 | }; 7 | 8 | const me = Object.create(person); 9 | 10 | me.name = "Matthew"; // "name" is a property set on "me", but not on "person" 11 | me.isHuman = true; // inherited properties can be overwritten 12 | 13 | me.printIntroduction(); 14 | // expected output: "My name is Matthew. Am I human? true" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-construct.js: -------------------------------------------------------------------------------- 1 | function monster1(disposition) { 2 | this.disposition = disposition; 3 | } 4 | 5 | const handler1 = { 6 | construct(target, args) { 7 | console.log('monster1 constructor called'); 8 | // expected output: "monster1 constructor called" 9 | 10 | return new target(...args); 11 | } 12 | }; 13 | 14 | const proxy1 = new Proxy(monster1, handler1); 15 | 16 | console.log(new proxy1('fierce').disposition); 17 | // expected output: "fierce" -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-tostring.js: -------------------------------------------------------------------------------- 1 | console.log(new RegExp('a+b+c')); 2 | // expected output: /a+b+c/ 3 | 4 | console.log(new RegExp('a+b+c').toString()); 5 | // expected output: "/a+b+c/" 6 | 7 | console.log(new RegExp('bar', 'g').toString()); 8 | // expected output: "/bar/g" 9 | 10 | console.log(new RegExp('\n', 'g').toString()); 11 | // expected output (if your browser supports escaping): "/\n/g" 12 | 13 | console.log(new RegExp('\\n', 'g').toString()); 14 | // expected output: "/\n/g" -------------------------------------------------------------------------------- /JavaScript/sharedarraybuffer/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "sharedarraybuffer-bytelength.js", 4 | "title": "JavaScript: SharedArrayBuffer.byteLength", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "sharedarraybuffer-constructor.js", 9 | "title": "JavaScript: SharedArrayBuffer Constructor", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "sharedarraybuffer-slice.js", 14 | "title": "JavaScript: SharedArrayBuffer.slice()", 15 | "type": "js" 16 | } 17 | ] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # interactive-examples 2 | 3 | Home of the [Quokka](https://quokkajs.com/) interactive code examples. 4 | 5 | These samples are curated JavaScript/TypeScript samples that are available in Quokka.js to 6 | quickly launch and run reference snippets. 7 | 8 | ## Contributing 9 | 10 | If you're interested in contributing to this project, great! Please create a pull request. 11 | Please note that all snippets need to be curated to ensure that they do not cause undesired 12 | runtime side-effects. -------------------------------------------------------------------------------- /JavaScript/array/array-reverse.js: -------------------------------------------------------------------------------- 1 | const array1 = ['one', 'two', 'three']; 2 | console.log('array1:', array1); 3 | // expected output: "array1:" Array ["one", "two", "three"] 4 | 5 | const reversed = array1.reverse(); 6 | console.log('reversed:', reversed); 7 | // expected output: "reversed:" Array ["three", "two", "one"] 8 | 9 | /* Careful: reverse is destructive. It also changes 10 | the original array */ 11 | console.log('array1:', array1); 12 | // expected output: "array1:" Array ["three", "two", "one"] -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-@@matchall.js: -------------------------------------------------------------------------------- 1 | class MyRegExp extends RegExp { 2 | [Symbol.matchAll](str) { 3 | let result = RegExp.prototype[Symbol.matchAll].call(this, str); 4 | if (!result) { 5 | return null; 6 | } 7 | return Array.from(result); 8 | } 9 | } 10 | 11 | let re = new MyRegExp('-[0-9]+', 'g'); 12 | console.log('2016-01-02|2019-03-07'.matchAll(re)); 13 | // expected output: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]] -------------------------------------------------------------------------------- /JavaScript/symbol/symbol-match.js: -------------------------------------------------------------------------------- 1 | const regexp1 = /foo/; 2 | // console.log('/foo/'.startsWith(regexp1)); 3 | // expected output: (Chrome) Error: First argument to String.prototype.startsWith must not be a regular expression 4 | // expected output: (Firefox) Error: Invalid type: first can't be a Regular Expression 5 | 6 | regexp1[Symbol.match] = false; 7 | 8 | console.log('/foo/'.startsWith(regexp1)); 9 | // expected output: true 10 | 11 | console.log('/baz/'.endsWith(regexp1)); 12 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/intl/intl-numberformat-prototype-format.js: -------------------------------------------------------------------------------- 1 | var amount = 654321.987; 2 | 3 | var options1 = { style: 'currency', currency: 'RUB' }; 4 | var numberFormat1 = new Intl.NumberFormat('ru-RU', options1); 5 | 6 | console.log(numberFormat1.format(amount)); 7 | // expected output: "654 321,99 ₽" 8 | 9 | var options2 = { style: 'currency', currency: 'USD' }; 10 | var numberFormat2 = new Intl.NumberFormat('en-US', options2); 11 | 12 | console.log(numberFormat2.format(amount)); 13 | // expected output: "$654,321.99" -------------------------------------------------------------------------------- /JavaScript/json/json-stringify.js: -------------------------------------------------------------------------------- 1 | console.log(JSON.stringify({ x: 5, y: 6 })); 2 | // expected output: "{"x":5,"y":6}" 3 | 4 | console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)])); 5 | // expected output: "[3,"false",false]" 6 | 7 | console.log(JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] })); 8 | // expected output: "{"x":[10,null,null,null]}" 9 | 10 | console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))); 11 | // expected output: ""2006-01-02T15:04:05.000Z"" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-getprototypeof.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | eyeCount: 4 3 | }; 4 | 5 | const monsterPrototype = { 6 | eyeCount : 2 7 | }; 8 | 9 | const handler = { 10 | getPrototypeOf(target) { 11 | return monsterPrototype; 12 | } 13 | }; 14 | 15 | const proxy1 = new Proxy(monster1, handler); 16 | 17 | console.log(Object.getPrototypeOf(proxy1) === monsterPrototype); 18 | // expected output: true 19 | 20 | console.log(Object.getPrototypeOf(proxy1).eyeCount); 21 | // expected output: 2 -------------------------------------------------------------------------------- /JavaScript/classes/classes-extends.js: -------------------------------------------------------------------------------- 1 | class formatDate extends Date { 2 | constructor(dateStr) { 3 | super(dateStr); 4 | } 5 | 6 | getFormattedDate() { 7 | const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 8 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 9 | 10 | return `${this.getDate()}-${months[this.getMonth()]}-${this.getFullYear()}`; 11 | } 12 | } 13 | 14 | console.log(new formatDate('August 19, 1975 23:15:30').getFormattedDate()); 15 | // expected output: "19-Aug-1975" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-getownpropertydescriptor.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | eyeCount: 4 3 | }; 4 | 5 | const handler1 = { 6 | getOwnPropertyDescriptor(target, prop) { 7 | console.log(`called: ${prop}`); 8 | // expected output: "called: eyeCount" 9 | 10 | return { configurable: true, enumerable: true, value: 5 }; 11 | } 12 | }; 13 | 14 | const proxy1 = new Proxy(monster1, handler1); 15 | 16 | console.log(Object.getOwnPropertyDescriptor(proxy1, 'eyeCount').value); 17 | // expected output: 5 -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-preventextensions.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | canEvolve: true 3 | }; 4 | 5 | const handler1 = { 6 | preventExtensions(target) { 7 | target.canEvolve = false; 8 | Object.preventExtensions(target); 9 | return true; 10 | } 11 | }; 12 | 13 | const proxy1 = new Proxy(monster1, handler1); 14 | 15 | console.log(monster1.canEvolve); 16 | // expected output: true 17 | 18 | Object.preventExtensions(proxy1); 19 | 20 | console.log(monster1.canEvolve); 21 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-apply.js: -------------------------------------------------------------------------------- 1 | function sum(a, b) { 2 | return a + b; 3 | } 4 | 5 | const handler = { 6 | apply: function(target, thisArg, argumentsList) { 7 | console.log(`Calculate sum: ${argumentsList}`); 8 | // expected output: "Calculate sum: 1,2" 9 | 10 | return target(argumentsList[0], argumentsList[1]) * 10; 11 | } 12 | }; 13 | 14 | var proxy1 = new Proxy(sum, handler); 15 | 16 | console.log(sum(1, 2)); 17 | // expected output: 3 18 | console.log(proxy1(1, 2)); 19 | // expected output: 30 -------------------------------------------------------------------------------- /JavaScript/intl/intl-relativetimeformat-prototype-resolvedoptions.js: -------------------------------------------------------------------------------- 1 | var rtf1 = new Intl.RelativeTimeFormat('en', { style: 'narrow' }); 2 | var options1 = rtf1.resolvedOptions(); 3 | 4 | var rtf2 = new Intl.RelativeTimeFormat('es', { numeric: 'auto' }); 5 | var options2 = rtf2.resolvedOptions(); 6 | 7 | console.log(`${options1.locale}, ${options1.style}, ${options1.numeric}`); 8 | // expected output: "en, narrow, always" 9 | 10 | console.log(`${options2.locale}, ${options2.style}, ${options2.numeric}`); 11 | // expected output: "es, long, auto" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-has.js: -------------------------------------------------------------------------------- 1 | const handler1 = { 2 | has (target, key) { 3 | if (key[0] === '_') { 4 | return false; 5 | } 6 | return key in target; 7 | } 8 | }; 9 | 10 | const monster1 = { 11 | _secret: 'easily scared', 12 | eyeCount: 4 13 | }; 14 | 15 | const proxy1 = new Proxy(monster1, handler1); 16 | console.log('eyeCount' in proxy1); 17 | // expected output: true 18 | 19 | console.log('_secret' in proxy1); 20 | // expected output: false 21 | 22 | console.log('_secret' in monster1); 23 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/intl/intl-numberformat.js: -------------------------------------------------------------------------------- 1 | var number = 123456.789; 2 | 3 | console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number)); 4 | // expected output: "123.456,79 €" 5 | 6 | // the Japanese yen doesn't use a minor unit 7 | console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number)); 8 | // expected output: "¥123,457" 9 | 10 | // limit to three significant digits 11 | console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number)); 12 | // expected output: "1,23,000" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-get.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | secret: 'easily scared', 3 | eyeCount: 4 4 | }; 5 | 6 | const handler1 = { 7 | get: function(target, prop, receiver) { 8 | if (prop === 'secret') { 9 | return `${target.secret.substr(0, 4)} ... shhhh!`; 10 | } else { 11 | return Reflect.get(...arguments); 12 | } 13 | } 14 | }; 15 | 16 | const proxy1 = new Proxy(monster1, handler1); 17 | 18 | console.log(proxy1.eyeCount); 19 | // expected output: 4 20 | 21 | console.log(proxy1.secret); 22 | // expected output: "easi ... shhhh!" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-defineproperty.js: -------------------------------------------------------------------------------- 1 | const handler1 = { 2 | defineProperty(target, key, descriptor) { 3 | invariant(key, 'define'); 4 | return true; 5 | } 6 | }; 7 | 8 | function invariant(key, action) { 9 | if (key[0] === '_') { 10 | throw new Error(`Invalid attempt to ${action} private "${key}" property`); 11 | } 12 | } 13 | 14 | const monster1 = {}; 15 | const proxy1 = new Proxy(monster1, handler1); 16 | 17 | console.log(proxy1._secret = 'easily scared'); 18 | // expected output: Error: Invalid attempt to define private "_secret" property -------------------------------------------------------------------------------- /JavaScript/regexp/regexp-prototype-test.js: -------------------------------------------------------------------------------- 1 | var str = 'table football'; 2 | 3 | var regex = RegExp('foo*'); 4 | var globalRegex = RegExp('foo*','g'); 5 | 6 | console.log(regex.test(str)); 7 | // expected output: true 8 | 9 | console.log(regex.test(str)); 10 | // expected output: true 11 | 12 | console.log(globalRegex.lastIndex); 13 | // expected output: 0 14 | 15 | console.log(globalRegex.test(str)); 16 | // expected output: true 17 | 18 | console.log(globalRegex.lastIndex); 19 | // expected output: 9 20 | 21 | console.log(globalRegex.test(str)); 22 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/string/string-indexof.js: -------------------------------------------------------------------------------- 1 | var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; 2 | 3 | var searchTerm = 'dog'; 4 | var indexOfFirst = paragraph.indexOf(searchTerm); 5 | 6 | console.log('The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst); 7 | // expected output: "The index of the first "dog" from the beginning is 40" 8 | 9 | console.log('The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf(searchTerm, (indexOfFirst + 1))); 10 | // expected output: "The index of the 2nd "dog" is 52" -------------------------------------------------------------------------------- /JavaScript/date/date-tolocaledatestring.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); 4 | 5 | const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; 6 | 7 | console.log(event.toLocaleDateString('de-DE', options)); 8 | // expected output: Donnerstag, 20. Dezember 2012 9 | 10 | console.log(event.toLocaleDateString('ar-EG', options)); 11 | // expected output: الخميس، ٢٠ ديسمبر، ٢٠١٢ 12 | 13 | console.log(event.toLocaleDateString('ko-KR', options)); 14 | // expected output: 2012년 12월 20일 목요일 -------------------------------------------------------------------------------- /JavaScript/intl/intl-datetimeformat.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); 4 | // Results below assume UTC timezone - your results may vary 5 | 6 | console.log(new Intl.DateTimeFormat('en-US').format(date)); 7 | // expected output: "12/20/2012" 8 | 9 | console.log(new Intl.DateTimeFormat('en-GB').format(date)); 10 | // expected output: "20/12/2012" 11 | 12 | // Include a fallback language, in this case Indonesian 13 | console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date)); 14 | // expected output: "20/12/2012" -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-deleteproperty.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | texture: 'scaly' 3 | }; 4 | 5 | const handler1 = { 6 | deleteProperty(target, prop) { 7 | if (prop in target) { 8 | delete target[prop]; 9 | console.log(`property removed: ${prop}`); 10 | // expected output: "property removed: texture" 11 | } 12 | } 13 | }; 14 | 15 | console.log(monster1.texture); 16 | // expected output: "scaly" 17 | 18 | const proxy1 = new Proxy(monster1, handler1); 19 | delete proxy1.texture; 20 | 21 | console.log(monster1.texture); 22 | // expected output: undefined -------------------------------------------------------------------------------- /JavaScript/arraybuffer/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "arraybuffer-constructor.js", 4 | "title": "JavaScript: ArrayBuffer Constructor", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "arraybuffer-bytelength.js", 9 | "title": "JavaScript: ArrayBuffer.byteLength", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "arraybuffer-isview.js", 14 | "title": "JavaScript: ArrayBuffer.isView()", 15 | "type": "js" 16 | }, 17 | { 18 | "fileName": "arraybuffer-slice.js", 19 | "title": "JavaScript: ArrayBuffer.slice()", 20 | "type": "js" 21 | } 22 | ] -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-setprototypeof.js: -------------------------------------------------------------------------------- 1 | const handler1 = { 2 | setPrototypeOf(monster1, monsterProto) { 3 | monster1.geneticallyModified = true; 4 | return false; 5 | } 6 | }; 7 | 8 | const monsterProto = {}; 9 | const monster1 = { 10 | geneticallyModified : false 11 | }; 12 | 13 | const proxy1 = new Proxy(monster1, handler1); 14 | // Object.setPrototypeOf(proxy1, monsterProto); // throws a TypeError 15 | 16 | console.log(Reflect.setPrototypeOf(proxy1, monsterProto)); 17 | // expected output: false 18 | 19 | console.log(monster1.geneticallyModified); 20 | // expected output: true -------------------------------------------------------------------------------- /JavaScript/weakmap/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "weakmap-prototype-delete.js", 4 | "title": "JavaScript: WeakMap.prototype.delete()", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "weakmap-prototype-get.js", 9 | "title": "JavaScript: WeakMap.prototype.get()", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "weakmap-prototype-has.js", 14 | "title": "JavaScript: WeakMap.prototype.has()", 15 | "type": "js" 16 | }, 17 | { 18 | "fileName": "weakmap-prototype-set.js", 19 | "title": "JavaScript: WeakMap.prototype.set()", 20 | "type": "js" 21 | } 22 | ] -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-set.js: -------------------------------------------------------------------------------- 1 | function Monster() { 2 | this.eyeCount = 4; 3 | } 4 | 5 | const handler1 = { 6 | set(obj, prop, value) { 7 | if ((prop === 'eyeCount') && ((value % 2) !== 0)) { 8 | console.log('Monsters must have an even number of eyes'); 9 | } else { 10 | return Reflect.set(...arguments); 11 | } 12 | } 13 | }; 14 | 15 | const monster1 = new Monster(); 16 | const proxy1 = new Proxy(monster1, handler1); 17 | proxy1.eyeCount = 1; 18 | // expected output: "Monsters must have an even number of eyes" 19 | 20 | console.log(proxy1.eyeCount); 21 | // expected output: 4 -------------------------------------------------------------------------------- /JavaScript/intl/intl-listformat.js: -------------------------------------------------------------------------------- 1 | const vehicles = ['Motorcycle', 'Bus', 'Car']; 2 | 3 | const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }); 4 | console.log(formatter.format(vehicles)); 5 | // expected output: "Motorcycle, Bus, and Car" 6 | 7 | const formatter2 = new Intl.ListFormat('de', { style: 'short', type: 'disjunction' }); 8 | console.log(formatter2.format(vehicles)); 9 | // expected output: "Motorcycle, Bus or Car" 10 | 11 | const formatter3 = new Intl.ListFormat('en', { style: 'narrow', type: 'unit' }); 12 | console.log(formatter3.format(vehicles)); 13 | // expected output: "Motorcycle Bus Car" -------------------------------------------------------------------------------- /JavaScript/intl/intl-datetimeformat-prototype-format.js: -------------------------------------------------------------------------------- 1 | // Note: this snippet only works in the browser 2 | 3 | var options1 = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; 4 | var date1 = new Date(2012, 05); 5 | 6 | var dateTimeFormat1 = new Intl.DateTimeFormat('sr-RS', options1); 7 | console.log(dateTimeFormat1.format(date1)); 8 | // expected output: "петак, 1. јун 2012." 9 | 10 | var dateTimeFormat2 = new Intl.DateTimeFormat('en-GB', options1); 11 | console.log(dateTimeFormat2.format(date1)); 12 | 13 | var dateTimeFormat3 = new Intl.DateTimeFormat('en-US', options1); 14 | console.log(dateTimeFormat3.format(date1)); -------------------------------------------------------------------------------- /JavaScript/statement/statement-async-function-asterisk.js: -------------------------------------------------------------------------------- 1 | async function* randomNumbers() { 2 | const url = 'https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'; 3 | 4 | console.log("Print randomly generated numbers until reset is clicked, or the random number exceeds 0.95"); 5 | while (true) { 6 | const response = await fetch(url); 7 | const text = await response.text(); 8 | yield Number(text); 9 | } 10 | } 11 | 12 | async function printRandoms() { 13 | for await (const number of randomNumbers()) { 14 | console.log(number); 15 | if (number > 0.95) { break; } 16 | } 17 | } 18 | 19 | printRandoms(); -------------------------------------------------------------------------------- /JavaScript/string/string-normalize.js: -------------------------------------------------------------------------------- 1 | var first = '\u212B'; // "Å" 2 | var second = '\u0041\u030A'; // "Å" 3 | 4 | console.log(first + ' and ' + second + ' are' + 5 | ((first === second)? '': ' not') + ' the same.'); 6 | // expected output: "Å and Å are not the same." 7 | 8 | console.log(first + ' and ' + second + ' can' + 9 | ((first.normalize('NFC') === second.normalize('NFC'))? '': ' not') + ' be normalized'); 10 | // expected output: "Å and Å can be normalized" 11 | 12 | var oldWord = 'mañana'; 13 | var newWord = oldWord.normalize('NFD'); 14 | console.log('The word did ' + ((oldWord != newWord)? '' : 'not ') + 'change.'); 15 | // expected output: "The word did change." -------------------------------------------------------------------------------- /JavaScript/bigint/meta.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "fileName": "bigint-asintn.js", 4 | "title": "JavaScript: BigInt.asIntN()", 5 | "type": "js" 6 | }, 7 | { 8 | "fileName": "bigint-asuintn.js", 9 | "title": "JavaScript: BigInt.asUintN()", 10 | "type": "js" 11 | }, 12 | { 13 | "fileName": "bigint-tolocalestring.js", 14 | "title": "JavaScript: BigInt.toLocaleString()", 15 | "type": "js" 16 | }, 17 | { 18 | "fileName": "bigint-tostring.js", 19 | "title": "JavaScript: BigInt.toString()", 20 | "type": "js" 21 | }, 22 | { 23 | "fileName": "bigint-valueof.js", 24 | "title": "JavaScript: BigInt.valueOf()", 25 | "type": "js" 26 | } 27 | ] -------------------------------------------------------------------------------- /JavaScript/string/string-repeat.js: -------------------------------------------------------------------------------- 1 | var chorus = 'Because I\'m happy. '; 2 | 3 | console.log('Chorus lyrics for "Happy": ' + chorus.repeat(27)); 4 | 5 | // expected output: "Chorus lyrics for "Happy": Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. " -------------------------------------------------------------------------------- /JavaScript/intl/intl-datetimeformat-prototype-formatrange.js: -------------------------------------------------------------------------------- 1 | var options1 = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; 2 | var options2 = { year: '2-digit', month: 'numeric', day: 'numeric' }; 3 | 4 | var startDate = new Date(Date.UTC(2007, 0, 10, 10, 0, 0)); 5 | var endDate = new Date(Date.UTC(2008, 0, 10, 11, 0, 0)); 6 | 7 | var dateTimeFormat = new Intl.DateTimeFormat('en', options1); 8 | console.log(dateTimeFormat.formatRange(startDate, endDate)); 9 | // expected output: Wednesday, January 10, 2007 – Thursday, January 10, 2008 10 | 11 | var dateTimeFormat2 = new Intl.DateTimeFormat("en", options2); 12 | console.log(dateTimeFormat2.formatRange(startDate, endDate)); 13 | // expected output: 1/10/07 – 1/10/08 -------------------------------------------------------------------------------- /JavaScript/proxyhandler/proxyhandler-isextensible.js: -------------------------------------------------------------------------------- 1 | const monster1 = { 2 | canEvolve: true 3 | }; 4 | 5 | const handler1 = { 6 | isExtensible(target) { 7 | return Reflect.isExtensible(target); 8 | }, 9 | preventExtensions(target) { 10 | target.canEvolve = false; 11 | return Reflect.preventExtensions(target); 12 | } 13 | }; 14 | 15 | const proxy1 = new Proxy(monster1, handler1); 16 | 17 | console.log(Object.isExtensible(proxy1)); 18 | // expected output: true 19 | 20 | console.log(monster1.canEvolve); 21 | // expected output: true 22 | 23 | Object.preventExtensions(proxy1); 24 | 25 | console.log(Object.isExtensible(proxy1)); 26 | // expected output: false 27 | 28 | console.log(monster1.canEvolve); 29 | // expected output: false -------------------------------------------------------------------------------- /JavaScript/statement/statement-async-for-in.js: -------------------------------------------------------------------------------- 1 | async function getFromList() { 2 | const url = 'https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'; 3 | 4 | const arrayOfFetches = [ 5 | doFetch(url), 6 | doFetch(url), 7 | doFetch(url) 8 | ]; 9 | 10 | console.log('Regular iterator: promise objects are expected.'); 11 | for (const item of arrayOfFetches) { 12 | console.log(item); 13 | } 14 | 15 | console.log('Async iterator: values from resolved promises are expected.'); 16 | for await (const item of arrayOfFetches) { 17 | console.log(item); 18 | } 19 | } 20 | 21 | async function doFetch(url) { 22 | const response = await fetch(url); 23 | const text = await response.text(); 24 | return Number(text); 25 | } 26 | 27 | getFromList(); --------------------------------------------------------------------------------