{"id":19784761,"url":"https://github.com/liushuping/what-you-should-know-about-javascript","last_synced_at":"2026-02-04T15:39:22.258Z","repository":{"id":34100112,"uuid":"37926297","full_name":"liushuping/what-you-should-know-about-javascript","owner":"liushuping","description":"What you should know about JavaScript: Comprehensive JavaScript tips","archived":false,"fork":false,"pushed_at":"2015-07-03T14:51:09.000Z","size":172,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T08:06:33.757Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liushuping.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-23T15:09:50.000Z","updated_at":"2015-06-23T15:09:50.000Z","dependencies_parsed_at":"2022-07-29T20:09:44.365Z","dependency_job_id":null,"html_url":"https://github.com/liushuping/what-you-should-know-about-javascript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/liushuping/what-you-should-know-about-javascript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liushuping%2Fwhat-you-should-know-about-javascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liushuping%2Fwhat-you-should-know-about-javascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liushuping%2Fwhat-you-should-know-about-javascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liushuping%2Fwhat-you-should-know-about-javascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liushuping","download_url":"https://codeload.github.com/liushuping/what-you-should-know-about-javascript/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liushuping%2Fwhat-you-should-know-about-javascript/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29088610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T03:31:03.593Z","status":"ssl_error","status_checked_at":"2026-02-04T03:29:50.742Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T06:12:33.517Z","updated_at":"2026-02-04T15:39:22.220Z","avatar_url":"https://github.com/liushuping.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# What you should know about JavaScript\nWhat you should know about JavaScript: Comprehensive JavaScript tips\n\n## Table Of Contents\n  1. [Array](#array)\n    1. [Truncate an array](#truncate-an-array)\n    2. [Comma in array literal decaration](#comma-in-array-literal-declaration)\n    3. [Array is a stack](#array-is-a-stack)\n    4. [Array is a queue](#array-is-a-queue)\n    5. [Array is a vector](#array-is-a-vector)\n  2. [Class](#class)\n    1. [Robust class declaration](#robust-class-declaration)\n  3. [String](#string)\n    1. [Encoded in UTF32](#encoded-in-utf32)\n  4. [Date](#date)\n    1. [Month and Date](#month-and-date)\n    2. [Timezone offset](#timezone-offset)\n  4. [Regex](#regex)\n  5. [Number](#number)\n  6. [Object](#object)\n    1. [A clean hash table](#a-clean-hash-table)\n    2. [Read-only property](#read-only-property)\n  7. [Function](#function)\n  8. [Bitwise](#bitwise)\n    1. [Zero-fill right shift](#zero-fill-right-shift)\n    2. [Get integral part of a decimal number](#get-integral-part-of-a-decimal-number)\n  9. [IIFE](#iife)\n  10. Miscs (#miscs)\n    1. [undefined and void 0](#undefined-and-void-0)\n    2. [!0 and !1](#0-and-1)\n  \n## Array\n#### Truncate an array\nTo truncate an array from end, it's not necessary to pop up one by one from the end, but setting the `length` property will achieve it.\n```javascript\n/* truncate an array */\nvar arr = [1, 2, 3, 4, 5];\narr.length = 2;\nconsole.log(arr); //[1, 2];\n``` \n#### Comma in array literal declaration\nIn an array literal declaration, if there is no value or element between the last comma `,` and `]`, then the last comma will be ignored.\n```javascript\n/* the last comma is ignored */\nvar arr = [1, 2, 3, 4, ];\nconsole.log(arr); //[1, 2, 3, 4]\n```\nIf there is no value or element between two commas, an `undefined` will be inserted there.\n```javascript\n/* two adjacent commas generate an undefined value */\nvar arr = [1, 2, , 3];\nconsole.log(arr); //[1, 2, undefined, 3]\n```\n\n#### Array is a stack\n#### Array is a queue\n#### Array is a vector\n\n## Class\n#### Robust class declaration\nThere is no difference between declare a JavaScript normal function and a class function, and to reduce user misuse of a class function as a normal function Pascal naming convention is one of the solution. However, this requires the user to follow the rule and not a technical solution. What we need is a robust class declaration, with it user hardly misuse it: use or not use `new` operation on that class will both generate a correct instance of that class.\n\nWe can use `this` keyword to detect whether it is pointing to an instance of that class, and if not create a new instance and return it to user.\n```javascript\n/* a robust class declaration */\nfunction Customer() {\n    if (!(this instanceof Customer)) {\n        return new Customer();\n    }\n}\n```\n\nTo be more generic, `arguments.callee` can be used to replace the `class` name.\n```javascript\n/* a generic robust class declaration */\nfunction Customer() {\n    if (!(this instanceof arguments.callee)) {\n        return new arguments.callee();\n    }\n}\n```\n\nThe code works on parameter-less classes, but it does not work on any class with parameters because it throws away the parameters. So, we need to restore the parameters.\n```javascript\nfunction Customer(name) {\n    if (!(this instanceof Customer)) {\n        return new Customer(name);\n    }\n}\n```\n\nBut if the class accepts length varying parameters, we cannot simply pass the `arguments` parameter to the internal class instantiation statement, but need handle it element by element.\n\n## String\n#### Encoded in UTF32\nJavaScript string is encoded in UTF32, so it supports the whole set of unicode characters.\n\n## Date\n#### Month and date\nJavaScript month starts from 0, but JavaScript date starts from 1.\n```javascript\nvar x = new Date();\nx.getMonth(); // starts from 0;\nx.getDate(); // starts from 1;\n```\n\n#### Timezone offset\nJavaScript `Date` object calculates (`getTimezoneOffset()`) by substracting local time from UTC time. So if local time zone is positive the result will be negative.\n\n## Regex\n## Number\n## Object\n#### A clean hash table\nBy nature, a JavaScript object is a hash table, and we can use it for most scenarios. However, it is not clean because it has some predefined keys there and if there will be conflicts if your data has the same key.\n```javascript\nvar hashtable = {}\nhashtable.hasOwnProperty; // function hasOwnProperty()\n```\nBesides the literal object declaration, an object could also be created using `Object` factory method.\n```javascript\n/* A definition of clean JavaScript hash table */\nvar hashtable = Object.create(null);\nhashtable.hasOwnProperty; // undefined\n```\n\n#### Read-only property\nA property defined using `this.some_property` or using object notation is write-able. But to define a read-only property, we can use `Object.defineProperty` method to achieve it.\n```javascript\nvar obj = {};\nObject.defineProperty(obj, 'name', {\n    get: function() {\n        return 'abc';\n    }\n});\n\nconsole.log(obj.name); // 'abc'\nobj.name = 123;\nconsole.log(obj.name); // 'abc'\n```\n\n## Function\n\n\n## Bitwise\n#### Zero-fill right shift\nThe right shift operator `\u003e\u003e` does not change the sign bit of a number, but zero-fill right shift operator can be used to change a negative number into a positive number.\n```javascript\n/* 5: 101 (base 2, without sign bit: 0)\n   -5: 11111111111111111111111111111011 (base 2, without sign bit: 1)\n   4294967291: 11111111111111111111111111111011 (base 2, without sign bit: 0\n*/\n-5 \u003e\u003e\u003e 0; // 4294967291\n```\nZero-fill right shift enables an interesting feature - output the actual binary representation for a negative number: complement code. With the `Number.prototype.toString(2)` method, we could not get the actual binary representation for a negative number.\n```javascript\n(-5).toString(2); // \"-101\"\n```\nBut use `\u003e\u003e\u003e` operator to change the sign bit to `0` then we can output the actual binary representation for a negative number.\n```javascript\n(-5\u003e\u003e\u003e0).toString(2); // \"11111111111111111111111111111011\"\n```\n\n#### Get integral part of a decimal number\nIn addition to the `parseInt` function to get the integral part of a number, but `\u003e\u003e` and `\u003c\u003c` operators can also be used to get the integral part of a decimal number.\n```javascript\n123 \u003c\u003c 0; // 123\n123.456 \u003c\u003c 0; // 123\n123.456 \u003e\u003e 0; // 123\n-456.789 \u003e\u003e 0; // -456\n```\n## IIFE\nThere are various forms of Immediately-Invoked Function Expressions, and 2 mostly used froms are\n```javascript\n/* form 1 */\n(function() {\n    // code\n})();\n\n/* form 2 */\n(function() {\n    // code\n}());\n```\nbut if you don't care about the function return value, there are manay other forms\n```javascript\n/* form 3 */\n!function() {\n    // code\n}();\n\n/* form 4 */\n~function() {\n    // code\n}();\n\n/* form 5 */\nvoid function() {\n    // code\n}();\n\n/* and more ... */\n```\n\n## Miscs\n#### undefined and void 0\n`void 0` equals to `undefined`, so you may find source code uses `void 0` to detect whether whether a variable is defined or not.\n\n#### !0 and !1\nFor code minimizing purpose, `!0` and `!1` are often be used to replace `true` and `false`\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliushuping%2Fwhat-you-should-know-about-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliushuping%2Fwhat-you-should-know-about-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliushuping%2Fwhat-you-should-know-about-javascript/lists"}