{"componentChunkName":"component---src-gatsby-theme-try-ghost-templates-post-js","path":"/linux-tips-for-beginners/","result":{"data":{"customPost":{"id":"Ghost__Post__6127ba1b3ed159214d382e73","title":"Linux Tips for Beginners","slug":"linux-tips-for-beginners","featured":false,"feature_image":"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1518432031352-d6fc5c10da5a.jpg","excerpt":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can help you use it efficiently.","custom_excerpt":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can help you use it efficiently.","visibility":"public","created_at_pretty":"10 Nov 2020","published_at_pretty":"10 Nov 2020","updated_at_pretty":"26 Aug 2021","created_at":"2020-11-10T08:53:51.000+00:00","published_at":"2020-11-10T09:52:57.000+00:00","updated_at":"2021-08-26T17:53:23.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":{"base":"photo-1521185496955-15097b20c5fe-2.jpeg","publicURL":"/static/13f953658eaecb779984996ea356b1c4/photo-1521185496955-15097b20c5fe-2.jpeg","imageMeta":{"width":2000,"height":1547},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABhU05c9sV/8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDABIR/9oACAEBAAEFAuTgGxGjVeRdPLOHf//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABkQAAMBAQEAAAAAAAAAAAAAAAABEXECEP/aAAgBAQAGPwL2dJaOqYVH/8QAGhAAAwADAQAAAAAAAAAAAAAAAAERITFBYf/aAAgBAQABPyFpWJH6IutKCacL+lRRGxiDm+H/2gAMAwEAAgADAAAAEK//AP/EABYRAQEBAAAAAAAAAAAAAAAAAAEREP/aAAgBAwEBPxAI3P/EABYRAQEBAAAAAAAAAAAAAAAAAAERAP/aAAgBAgEBPxBaR03/xAAbEAEBAAIDAQAAAAAAAAAAAAABEQAhMUGxcf/aAAgBAQABPxAInmDF9wQ6urfMI5JgA/si0141k9pEa4fcjFJmybz/2Q==","aspectRatio":1.2935323383084578,"src":"/static/13f953658eaecb779984996ea356b1c4/d5c54/photo-1521185496955-15097b20c5fe-2.jpg","srcSet":"/static/13f953658eaecb779984996ea356b1c4/65d8c/photo-1521185496955-15097b20c5fe-2.jpg 260w,\n/static/13f953658eaecb779984996ea356b1c4/c5f21/photo-1521185496955-15097b20c5fe-2.jpg 520w,\n/static/13f953658eaecb779984996ea356b1c4/d5c54/photo-1521185496955-15097b20c5fe-2.jpg 1040w,\n/static/13f953658eaecb779984996ea356b1c4/81a53/photo-1521185496955-15097b20c5fe-2.jpg 1560w,\n/static/13f953658eaecb779984996ea356b1c4/4e5f3/photo-1521185496955-15097b20c5fe-2.jpg 2000w","srcWebp":"/static/13f953658eaecb779984996ea356b1c4/e4875/photo-1521185496955-15097b20c5fe-2.webp","srcSetWebp":"/static/13f953658eaecb779984996ea356b1c4/dc8f3/photo-1521185496955-15097b20c5fe-2.webp 260w,\n/static/13f953658eaecb779984996ea356b1c4/2db4b/photo-1521185496955-15097b20c5fe-2.webp 520w,\n/static/13f953658eaecb779984996ea356b1c4/e4875/photo-1521185496955-15097b20c5fe-2.webp 1040w,\n/static/13f953658eaecb779984996ea356b1c4/f5845/photo-1521185496955-15097b20c5fe-2.webp 1560w,\n/static/13f953658eaecb779984996ea356b1c4/49d6b/photo-1521185496955-15097b20c5fe-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null},{"slug":"tips","url":"https://backend.shahednasser.com/tag/tips/","name":"Tips","visibility":"public","feature_image":null,"description":"Learn more about programming and development through these articles that have essential tips!","meta_title":"Tips on Technology and Programming","meta_description":null,"featureImageSharp":null}],"plaintext":"Many beginners find it hard to understand and adapt to Linux. In this post I\nwill list a few helpful tips for Linux that can get you started on how to use it\nefficiently.\n\n\n--------------------------------------------------------------------------------\n\nTerminal Auto Completion\nThis is probably an easy, obvious one to many of us but it is still important to\nlist. When using the terminal, you can use auto completion for commands or to\nuse a file/folder in the directory you're currently in by pressing the TAB key\non your keyboard. This is helpful if you're still new to linux, if you just\nforgot what the name of the command is, or if you're just too lazy to type it\nout (like many of us are).\n\n\n--------------------------------------------------------------------------------\n\nQuick Navigation Between Directories\nAs you know in Linux, to move from one directory to the other in the terminal\nyou use the cd command. For example:\n\ncd my-folder\n\nThere are three shortcuts that can help you navigate between directories:\n\n 1. Navigate to the Home directory: To move to the home directory, you just need\n    to use cd ~.\n 2. Navigate to the parent directory: To move to the parent directory, you just\n    need to use cd ..\n 3. Navigate to the previous directory: To move to the directory you were\n    previously in, you just need to use cd -\n\n\n--------------------------------------------------------------------------------\n\nMoving To the Beginning and End of Line\nWhen you are running commands in the terminal, especially when you are\ncopy/pasting the commands, a lot of time it can be a hassle when you want to\nedit something in the beginning of the line and then moving back to the end of\nit. There is an easier way to do it.\n\n 1. To move to the beginning of the line press CTRL + A\n 2. To move to the end of the line press CTRL + E\n\n\n--------------------------------------------------------------------------------\n\nRead a File As It Updates\nThis is very essential when you are reading from a log file. To keep executing \ntail every time you want to check for changes can be a hassle and frankly eye\nblinding.\n\nIn case you're not aware, tail is a command used to read the end of a file.\n\nSo to read a file and have it update as any new lines are added to the file,\njust add -f option to the command. For example:\n\ntail -f /var/logs/apache2/error.log\n\nThis can be used on any file as well, not just logs.\n\n\n--------------------------------------------------------------------------------\n\nReuse The Previous Parameter\nTo use the parameter you've used in the previous command, use !$. For example:\n\ntouch test.txt // creates a new file test.txt\nnano !$ // edit the previous parameter which is test.txt\n\n\n--------------------------------------------------------------------------------\n\nReuse The Previous Command\nTo reuse the previous command, use !!. For example\n\ntail -n 100 error.log\n!! //use the previous command again\n\nThis is mostly helpful when you run a command, but realize you need to add sudo \nto it. So instead of going to the beginning of the line to do that, just use \nsudo !!\n\n\n--------------------------------------------------------------------------------\n\nGet Information About Any Command\nIf you want to get information about any command like its description of the\noptions and parameters it accepts, simply add --help to it. For example:\n\ncd --help\n\n\n--------------------------------------------------------------------------------\n\nCopying and Pasting From The Terminal\nTo copy anything from the terminal, use CTRL + SHIFT + C\n\nTo paste anything to the terminal, use CTRL + SHIFT + V\n\n\n--------------------------------------------------------------------------------\n\nSearch Through The Commands\nIf you've used a command and you want to use it again, but you forgot some of it\nor you're just too lazy to type it out, one way to use it again is by searching\nin your terminal. To do that, press CTRL + R and then type in the command. As\nyou type you will see commands that match your search and when you find what you\nneed, press ENTER and the command will run.\n\n\n--------------------------------------------------------------------------------\n\nReading Files in Parts\nUse less to read a file a little by little. Many people use cat but if the file\nis big then using cat can be overwhelming. When you use less you can go through\nthe file in parts and at your own pace. Example of usage:\n\nless test.txt\n\nTo exit the file you are reading just type q.\n\n\n--------------------------------------------------------------------------------\n\nAnd There's Still Many More!\nIf you have any other linux tips that make your day less of a pain, please share\nthem with everyone below!","html":"<p>Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently.</p><hr><h2 id=\"terminal-auto-completion\">Terminal Auto Completion</h2><p>This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the <code>TAB</code> key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are).</p><hr><h2 id=\"quick-navigation-between-directories\">Quick Navigation Between Directories</h2><p>As you know in Linux, to move from one directory to the other in the terminal you use the <code>cd</code> command. For example:</p><pre><code class=\"language-Shell\">cd my-folder</code></pre><p>There are three shortcuts that can help you navigate between directories:</p><ol><li><strong>Navigate to the Home directory</strong>: To move to the home directory, you just need to use <code>cd ~</code>.</li><li><strong>Navigate to the parent directory: </strong>To move to the parent directory, you just need to use <code>cd ..</code></li><li><strong>Navigate to the previous directory: </strong>To move to the directory you were previously in, you just need to use <code>cd -</code></li></ol><hr><h2 id=\"moving-to-the-beginning-and-end-of-line\">Moving To the Beginning and End of Line</h2><p>When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it.</p><ol><li><strong>To move to the beginning of the line</strong> press <code>CTRL + A</code></li><li><strong>To move to the end of the line </strong>press <code>CTRL + E</code></li></ol><hr><h2 id=\"read-a-file-as-it-updates\">Read a File As It Updates</h2><p>This is very essential when you are reading from a log file. To keep executing <code>tail</code> every time you want to check for changes can be a hassle and frankly eye blinding.</p><p><em>In case you're not aware, tail is a command used to read the end of a file.</em></p><p>So to read a file and have it update as any new lines are added to the file, just add <code>-f</code> option to the command. For example:</p><pre><code class=\"language-Shell\">tail -f /var/logs/apache2/error.log</code></pre><p>This can be used on any file as well, not just logs.</p><hr><h2 id=\"reuse-the-previous-parameter\">Reuse The Previous Parameter</h2><p>To use the parameter you've used in the previous command, use <code>!$</code>. For example:</p><pre><code class=\"language-Shell\">touch test.txt // creates a new file test.txt\nnano !$ // edit the previous parameter which is test.txt</code></pre><hr><h2 id=\"reuse-the-previous-command\">Reuse The Previous Command</h2><p>To reuse the previous command, use <code>!!</code>. For example</p><pre><code class=\"language-Shell\">tail -n 100 error.log\n!! //use the previous command again</code></pre><p>This is mostly helpful when you run a command, but realize you need to add <code>sudo</code> to it. So instead of going to the beginning of the line to do that, just use <code>sudo !!</code></p><hr><h2 id=\"get-information-about-any-command\">Get Information About Any Command</h2><p>If you want to get information about any command like its description of the options and parameters it accepts, simply add <code>--help</code> to it. For example:</p><pre><code class=\"language-Shell\">cd --help</code></pre><hr><h2 id=\"copying-and-pasting-from-the-terminal\">Copying and Pasting From The Terminal</h2><p>To copy anything from the terminal, use <code>CTRL + SHIFT + C</code></p><p>To paste anything to the terminal, use <code>CTRL + SHIFT + V</code></p><hr><h2 id=\"search-through-the-commands\">Search Through The Commands</h2><p>If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press <code>CTRL + R</code> and then type in the command. As you type you will see commands that match your search and when you find what you need, press <code>ENTER</code> and the command will run.</p><hr><h2 id=\"reading-files-in-parts\">Reading Files in Parts</h2><p>Use <code>less</code> to read a file a little by little. Many people use <code>cat</code> but if the file is big then using <code>cat</code> can be overwhelming. When you use <code>less</code> you can go through the file in parts and at your own pace. Example of usage:</p><pre><code class=\"language-Shell\">less test.txt</code></pre><p>To exit the file you are reading just type <code>q</code>.</p><hr><h2 id=\"and-there-s-still-many-more-\">And There's Still Many More!</h2><p>If you have any other linux tips that make your day less of a pain, please share them with everyone below!</p>","url":"https://backend.shahednasser.com/linux-tips-for-beginners/","canonical_url":null,"uuid":"1d697af0-765a-43f0-b8ab-f88b337dc65f","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5faa551f2280bc001e2ccd1f","reading_time":3,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently.</p><hr><h2 id=\"terminal-auto-completion\">Terminal Auto Completion</h2><p>This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the <code class=\"language-text\">TAB</code> key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are).</p><hr><h2 id=\"quick-navigation-between-directories\">Quick Navigation Between Directories</h2><p>As you know in Linux, to move from one directory to the other in the terminal you use the <code class=\"language-text\">cd</code> command. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token builtin class-name\">cd</span> my-folder</code></pre></div><p>There are three shortcuts that can help you navigate between directories:</p><ol><li><strong>Navigate to the Home directory</strong>: To move to the home directory, you just need to use <code class=\"language-text\">cd ~</code>.</li><li><strong>Navigate to the parent directory: </strong>To move to the parent directory, you just need to use <code class=\"language-text\">cd ..</code></li><li><strong>Navigate to the previous directory: </strong>To move to the directory you were previously in, you just need to use <code class=\"language-text\">cd -</code></li></ol><hr><h2 id=\"moving-to-the-beginning-and-end-of-line\">Moving To the Beginning and End of Line</h2><p>When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it.</p><ol><li><strong>To move to the beginning of the line</strong> press <code class=\"language-text\">CTRL + A</code></li><li><strong>To move to the end of the line </strong>press <code class=\"language-text\">CTRL + E</code></li></ol><hr><h2 id=\"read-a-file-as-it-updates\">Read a File As It Updates</h2><p>This is very essential when you are reading from a log file. To keep executing <code class=\"language-text\">tail</code> every time you want to check for changes can be a hassle and frankly eye blinding.</p><p><em>In case you're not aware, tail is a command used to read the end of a file.</em></p><p>So to read a file and have it update as any new lines are added to the file, just add <code class=\"language-text\">-f</code> option to the command. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">tail</span> -f /var/logs/apache2/error.log</code></pre></div><p>This can be used on any file as well, not just logs.</p><hr><h2 id=\"reuse-the-previous-parameter\">Reuse The Previous Parameter</h2><p>To use the parameter you've used in the previous command, use <code class=\"language-text\">!$</code>. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">touch</span> test.txt // creates a new <span class=\"token function\">file</span> test.txt\n<span class=\"token function\">nano</span> <span class=\"token operator\">!</span>$ // edit the previous parameter <span class=\"token function\">which</span> is test.txt</code></pre></div><hr><h2 id=\"reuse-the-previous-command\">Reuse The Previous Command</h2><p>To reuse the previous command, use <code class=\"language-text\">!!</code>. For example</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">tail</span> -n <span class=\"token number\">100</span> error.log\n<span class=\"token operator\">!</span><span class=\"token operator\">!</span> //use the previous <span class=\"token builtin class-name\">command</span> again</code></pre></div><p>This is mostly helpful when you run a command, but realize you need to add <code class=\"language-text\">sudo</code> to it. So instead of going to the beginning of the line to do that, just use <code class=\"language-text\">sudo !!</code></p><hr><h2 id=\"get-information-about-any-command\">Get Information About Any Command</h2><p>If you want to get information about any command like its description of the options and parameters it accepts, simply add <code class=\"language-text\">--help</code> to it. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token builtin class-name\">cd</span> --help</code></pre></div><hr><h2 id=\"copying-and-pasting-from-the-terminal\">Copying and Pasting From The Terminal</h2><p>To copy anything from the terminal, use <code class=\"language-text\">CTRL + SHIFT + C</code></p><p>To paste anything to the terminal, use <code class=\"language-text\">CTRL + SHIFT + V</code></p><hr><h2 id=\"search-through-the-commands\">Search Through The Commands</h2><p>If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press <code class=\"language-text\">CTRL + R</code> and then type in the command. As you type you will see commands that match your search and when you find what you need, press <code class=\"language-text\">ENTER</code> and the command will run.</p><hr><h2 id=\"reading-files-in-parts\">Reading Files in Parts</h2><p>Use <code class=\"language-text\">less</code> to read a file a little by little. Many people use <code class=\"language-text\">cat</code> but if the file is big then using <code class=\"language-text\">cat</code> can be overwhelming. When you use <code class=\"language-text\">less</code> you can go through the file in parts and at your own pace. Example of usage:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">less</span> test.txt</code></pre></div><p>To exit the file you are reading just type <code class=\"language-text\">q</code>.</p><hr><h2 id=\"and-there-s-still-many-more-\">And There's Still Many More!</h2><p>If you have any other linux tips that make your day less of a pain, please share them with everyone below!</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"terminal-auto-completion"},"children":[{"type":"text","value":"Terminal Auto Completion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TAB"}]},{"type":"text","value":" key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are)."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"quick-navigation-between-directories"},"children":[{"type":"text","value":"Quick Navigation Between Directories"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you know in Linux, to move from one directory to the other in the terminal you use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" command. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" my-folder"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are three shortcuts that can help you navigate between directories:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the Home directory"}]},{"type":"text","value":": To move to the home directory, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd ~"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the parent directory: "}]},{"type":"text","value":"To move to the parent directory, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd .."}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the previous directory: "}]},{"type":"text","value":"To move to the directory you were previously in, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd -"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"moving-to-the-beginning-and-end-of-line"},"children":[{"type":"text","value":"Moving To the Beginning and End of Line"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"To move to the beginning of the line"}]},{"type":"text","value":" press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + A"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"To move to the end of the line "}]},{"type":"text","value":"press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + E"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"read-a-file-as-it-updates"},"children":[{"type":"text","value":"Read a File As It Updates"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is very essential when you are reading from a log file. To keep executing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" every time you want to check for changes can be a hassle and frankly eye blinding."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"In case you're not aware, tail is a command used to read the end of a file."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So to read a file and have it update as any new lines are added to the file, just add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-f"}]},{"type":"text","value":" option to the command. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" -f /var/logs/apache2/error.log"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This can be used on any file as well, not just logs."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reuse-the-previous-parameter"},"children":[{"type":"text","value":"Reuse The Previous Parameter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To use the parameter you've used in the previous command, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!$"}]},{"type":"text","value":". For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"touch"}]},{"type":"text","value":" test.txt // creates a new "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"file"}]},{"type":"text","value":" test.txt\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nano"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"$ // edit the previous parameter "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"which"}]},{"type":"text","value":" is test.txt"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reuse-the-previous-command"},"children":[{"type":"text","value":"Reuse The Previous Command"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To reuse the previous command, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!!"}]},{"type":"text","value":". For example"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" -n "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":" error.log\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":" //use the previous "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"command"}]},{"type":"text","value":" again"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is mostly helpful when you run a command, but realize you need to add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" to it. So instead of going to the beginning of the line to do that, just use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudo !!"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"get-information-about-any-command"},"children":[{"type":"text","value":"Get Information About Any Command"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to get information about any command like its description of the options and parameters it accepts, simply add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"--help"}]},{"type":"text","value":" to it. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" --help"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"copying-and-pasting-from-the-terminal"},"children":[{"type":"text","value":"Copying and Pasting From The Terminal"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To copy anything from the terminal, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + SHIFT + C"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To paste anything to the terminal, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + SHIFT + V"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"search-through-the-commands"},"children":[{"type":"text","value":"Search Through The Commands"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + R"}]},{"type":"text","value":" and then type in the command. As you type you will see commands that match your search and when you find what you need, press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ENTER"}]},{"type":"text","value":" and the command will run."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reading-files-in-parts"},"children":[{"type":"text","value":"Reading Files in Parts"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" to read a file a little by little. Many people use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" but if the file is big then using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" can be overwhelming. When you use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" you can go through the file in parts and at your own pace. Example of usage:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" test.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To exit the file you are reading just type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"q"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"and-there-s-still-many-more-"},"children":[{"type":"text","value":"And There's Still Many More!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have any other linux tips that make your day less of a pain, please share them with everyone below!"}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"terminal-auto-completion","heading":"Terminal Auto Completion"},{"id":"quick-navigation-between-directories","heading":"Quick Navigation Between Directories"},{"id":"moving-to-the-beginning-and-end-of-line","heading":"Moving To the Beginning and End of Line"},{"id":"read-a-file-as-it-updates","heading":"Read a File As It Updates"},{"id":"reuse-the-previous-parameter","heading":"Reuse The Previous Parameter"},{"id":"reuse-the-previous-command","heading":"Reuse The Previous Command"},{"id":"get-information-about-any-command","heading":"Get Information About Any Command"},{"id":"copying-and-pasting-from-the-terminal","heading":"Copying and Pasting From The Terminal"},{"id":"search-through-the-commands","heading":"Search Through The Commands"},{"id":"reading-files-in-parts","heading":"Reading Files in Parts"},{"id":"and-there-s-still-many-more-","heading":"And There's Still Many More!"}]},"featureImageSharp":{"base":"photo-1518432031352-d6fc5c10da5a.jpg","publicURL":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/photo-1518432031352-d6fc5c10da5a.jpg","imageMeta":{"width":2000,"height":1500},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAwAE/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAc2djoJaP//EABkQAQEBAAMAAAAAAAAAAAAAAAEAEQISIf/aAAgBAQABBQLuTZPPQ8lb/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGBAAAwEBAAAAAAAAAAAAAAAAABExARD/2gAIAQEABj8CnUh4U//EABoQAAMAAwEAAAAAAAAAAAAAAAABIRExQcH/2gAIAQEAAT8hn6QquhhfIHeUIZex/9oADAMBAAIAAwAAABAz/wD/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPxBH/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Qh//EABwQAQACAgMBAAAAAAAAAAAAAAEAESExQWGBsf/aAAgBAQABPxBQ0g84lVI3jiUOyZRCKywYoWmh+xjXN3P/2Q==","aspectRatio":1.3333333333333333,"src":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/d5c54/photo-1518432031352-d6fc5c10da5a.jpg","srcSet":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/65d8c/photo-1518432031352-d6fc5c10da5a.jpg 260w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/c5f21/photo-1518432031352-d6fc5c10da5a.jpg 520w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/d5c54/photo-1518432031352-d6fc5c10da5a.jpg 1040w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/81a53/photo-1518432031352-d6fc5c10da5a.jpg 1560w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/4e5f3/photo-1518432031352-d6fc5c10da5a.jpg 2000w","srcWebp":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/e4875/photo-1518432031352-d6fc5c10da5a.webp","srcSetWebp":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/dc8f3/photo-1518432031352-d6fc5c10da5a.webp 260w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/2db4b/photo-1518432031352-d6fc5c10da5a.webp 520w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/e4875/photo-1518432031352-d6fc5c10da5a.webp 1040w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/f5845/photo-1518432031352-d6fc5c10da5a.webp 1560w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/49d6b/photo-1518432031352-d6fc5c10da5a.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"ghostPost":{"id":"Ghost__Post__6127ba1b3ed159214d382e73","title":"Linux Tips for Beginners","slug":"linux-tips-for-beginners","featured":false,"feature_image":"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1518432031352-d6fc5c10da5a.jpg","excerpt":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can help you use it efficiently.","custom_excerpt":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can help you use it efficiently.","visibility":"public","created_at_pretty":"10 Nov 2020","published_at_pretty":"10 Nov 2020","updated_at_pretty":"26 Aug 2021","created_at":"2020-11-10T08:53:51.000+00:00","published_at":"2020-11-10T09:52:57.000+00:00","updated_at":"2021-08-26T17:53:23.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":{"base":"photo-1521185496955-15097b20c5fe-2.jpeg","publicURL":"/static/13f953658eaecb779984996ea356b1c4/photo-1521185496955-15097b20c5fe-2.jpeg","imageMeta":{"width":2000,"height":1547},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABhU05c9sV/8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDABIR/9oACAEBAAEFAuTgGxGjVeRdPLOHf//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABkQAAMBAQEAAAAAAAAAAAAAAAABEXECEP/aAAgBAQAGPwL2dJaOqYVH/8QAGhAAAwADAQAAAAAAAAAAAAAAAAERITFBYf/aAAgBAQABPyFpWJH6IutKCacL+lRRGxiDm+H/2gAMAwEAAgADAAAAEK//AP/EABYRAQEBAAAAAAAAAAAAAAAAAAEREP/aAAgBAwEBPxAI3P/EABYRAQEBAAAAAAAAAAAAAAAAAAERAP/aAAgBAgEBPxBaR03/xAAbEAEBAAIDAQAAAAAAAAAAAAABEQAhMUGxcf/aAAgBAQABPxAInmDF9wQ6urfMI5JgA/si0141k9pEa4fcjFJmybz/2Q==","aspectRatio":1.2935323383084578,"src":"/static/13f953658eaecb779984996ea356b1c4/d5c54/photo-1521185496955-15097b20c5fe-2.jpg","srcSet":"/static/13f953658eaecb779984996ea356b1c4/65d8c/photo-1521185496955-15097b20c5fe-2.jpg 260w,\n/static/13f953658eaecb779984996ea356b1c4/c5f21/photo-1521185496955-15097b20c5fe-2.jpg 520w,\n/static/13f953658eaecb779984996ea356b1c4/d5c54/photo-1521185496955-15097b20c5fe-2.jpg 1040w,\n/static/13f953658eaecb779984996ea356b1c4/81a53/photo-1521185496955-15097b20c5fe-2.jpg 1560w,\n/static/13f953658eaecb779984996ea356b1c4/4e5f3/photo-1521185496955-15097b20c5fe-2.jpg 2000w","srcWebp":"/static/13f953658eaecb779984996ea356b1c4/e4875/photo-1521185496955-15097b20c5fe-2.webp","srcSetWebp":"/static/13f953658eaecb779984996ea356b1c4/dc8f3/photo-1521185496955-15097b20c5fe-2.webp 260w,\n/static/13f953658eaecb779984996ea356b1c4/2db4b/photo-1521185496955-15097b20c5fe-2.webp 520w,\n/static/13f953658eaecb779984996ea356b1c4/e4875/photo-1521185496955-15097b20c5fe-2.webp 1040w,\n/static/13f953658eaecb779984996ea356b1c4/f5845/photo-1521185496955-15097b20c5fe-2.webp 1560w,\n/static/13f953658eaecb779984996ea356b1c4/49d6b/photo-1521185496955-15097b20c5fe-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null},{"slug":"tips","url":"https://backend.shahednasser.com/tag/tips/","name":"Tips","visibility":"public","feature_image":null,"description":"Learn more about programming and development through these articles that have essential tips!","meta_title":"Tips on Technology and Programming","meta_description":null,"featureImageSharp":null}],"plaintext":"Many beginners find it hard to understand and adapt to Linux. In this post I\nwill list a few helpful tips for Linux that can get you started on how to use it\nefficiently.\n\n\n--------------------------------------------------------------------------------\n\nTerminal Auto Completion\nThis is probably an easy, obvious one to many of us but it is still important to\nlist. When using the terminal, you can use auto completion for commands or to\nuse a file/folder in the directory you're currently in by pressing the TAB key\non your keyboard. This is helpful if you're still new to linux, if you just\nforgot what the name of the command is, or if you're just too lazy to type it\nout (like many of us are).\n\n\n--------------------------------------------------------------------------------\n\nQuick Navigation Between Directories\nAs you know in Linux, to move from one directory to the other in the terminal\nyou use the cd command. For example:\n\ncd my-folder\n\nThere are three shortcuts that can help you navigate between directories:\n\n 1. Navigate to the Home directory: To move to the home directory, you just need\n    to use cd ~.\n 2. Navigate to the parent directory: To move to the parent directory, you just\n    need to use cd ..\n 3. Navigate to the previous directory: To move to the directory you were\n    previously in, you just need to use cd -\n\n\n--------------------------------------------------------------------------------\n\nMoving To the Beginning and End of Line\nWhen you are running commands in the terminal, especially when you are\ncopy/pasting the commands, a lot of time it can be a hassle when you want to\nedit something in the beginning of the line and then moving back to the end of\nit. There is an easier way to do it.\n\n 1. To move to the beginning of the line press CTRL + A\n 2. To move to the end of the line press CTRL + E\n\n\n--------------------------------------------------------------------------------\n\nRead a File As It Updates\nThis is very essential when you are reading from a log file. To keep executing \ntail every time you want to check for changes can be a hassle and frankly eye\nblinding.\n\nIn case you're not aware, tail is a command used to read the end of a file.\n\nSo to read a file and have it update as any new lines are added to the file,\njust add -f option to the command. For example:\n\ntail -f /var/logs/apache2/error.log\n\nThis can be used on any file as well, not just logs.\n\n\n--------------------------------------------------------------------------------\n\nReuse The Previous Parameter\nTo use the parameter you've used in the previous command, use !$. For example:\n\ntouch test.txt // creates a new file test.txt\nnano !$ // edit the previous parameter which is test.txt\n\n\n--------------------------------------------------------------------------------\n\nReuse The Previous Command\nTo reuse the previous command, use !!. For example\n\ntail -n 100 error.log\n!! //use the previous command again\n\nThis is mostly helpful when you run a command, but realize you need to add sudo \nto it. So instead of going to the beginning of the line to do that, just use \nsudo !!\n\n\n--------------------------------------------------------------------------------\n\nGet Information About Any Command\nIf you want to get information about any command like its description of the\noptions and parameters it accepts, simply add --help to it. For example:\n\ncd --help\n\n\n--------------------------------------------------------------------------------\n\nCopying and Pasting From The Terminal\nTo copy anything from the terminal, use CTRL + SHIFT + C\n\nTo paste anything to the terminal, use CTRL + SHIFT + V\n\n\n--------------------------------------------------------------------------------\n\nSearch Through The Commands\nIf you've used a command and you want to use it again, but you forgot some of it\nor you're just too lazy to type it out, one way to use it again is by searching\nin your terminal. To do that, press CTRL + R and then type in the command. As\nyou type you will see commands that match your search and when you find what you\nneed, press ENTER and the command will run.\n\n\n--------------------------------------------------------------------------------\n\nReading Files in Parts\nUse less to read a file a little by little. Many people use cat but if the file\nis big then using cat can be overwhelming. When you use less you can go through\nthe file in parts and at your own pace. Example of usage:\n\nless test.txt\n\nTo exit the file you are reading just type q.\n\n\n--------------------------------------------------------------------------------\n\nAnd There's Still Many More!\nIf you have any other linux tips that make your day less of a pain, please share\nthem with everyone below!","html":"<p>Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently.</p><hr><h2 id=\"terminal-auto-completion\">Terminal Auto Completion</h2><p>This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the <code>TAB</code> key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are).</p><hr><h2 id=\"quick-navigation-between-directories\">Quick Navigation Between Directories</h2><p>As you know in Linux, to move from one directory to the other in the terminal you use the <code>cd</code> command. For example:</p><pre><code class=\"language-Shell\">cd my-folder</code></pre><p>There are three shortcuts that can help you navigate between directories:</p><ol><li><strong>Navigate to the Home directory</strong>: To move to the home directory, you just need to use <code>cd ~</code>.</li><li><strong>Navigate to the parent directory: </strong>To move to the parent directory, you just need to use <code>cd ..</code></li><li><strong>Navigate to the previous directory: </strong>To move to the directory you were previously in, you just need to use <code>cd -</code></li></ol><hr><h2 id=\"moving-to-the-beginning-and-end-of-line\">Moving To the Beginning and End of Line</h2><p>When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it.</p><ol><li><strong>To move to the beginning of the line</strong> press <code>CTRL + A</code></li><li><strong>To move to the end of the line </strong>press <code>CTRL + E</code></li></ol><hr><h2 id=\"read-a-file-as-it-updates\">Read a File As It Updates</h2><p>This is very essential when you are reading from a log file. To keep executing <code>tail</code> every time you want to check for changes can be a hassle and frankly eye blinding.</p><p><em>In case you're not aware, tail is a command used to read the end of a file.</em></p><p>So to read a file and have it update as any new lines are added to the file, just add <code>-f</code> option to the command. For example:</p><pre><code class=\"language-Shell\">tail -f /var/logs/apache2/error.log</code></pre><p>This can be used on any file as well, not just logs.</p><hr><h2 id=\"reuse-the-previous-parameter\">Reuse The Previous Parameter</h2><p>To use the parameter you've used in the previous command, use <code>!$</code>. For example:</p><pre><code class=\"language-Shell\">touch test.txt // creates a new file test.txt\nnano !$ // edit the previous parameter which is test.txt</code></pre><hr><h2 id=\"reuse-the-previous-command\">Reuse The Previous Command</h2><p>To reuse the previous command, use <code>!!</code>. For example</p><pre><code class=\"language-Shell\">tail -n 100 error.log\n!! //use the previous command again</code></pre><p>This is mostly helpful when you run a command, but realize you need to add <code>sudo</code> to it. So instead of going to the beginning of the line to do that, just use <code>sudo !!</code></p><hr><h2 id=\"get-information-about-any-command\">Get Information About Any Command</h2><p>If you want to get information about any command like its description of the options and parameters it accepts, simply add <code>--help</code> to it. For example:</p><pre><code class=\"language-Shell\">cd --help</code></pre><hr><h2 id=\"copying-and-pasting-from-the-terminal\">Copying and Pasting From The Terminal</h2><p>To copy anything from the terminal, use <code>CTRL + SHIFT + C</code></p><p>To paste anything to the terminal, use <code>CTRL + SHIFT + V</code></p><hr><h2 id=\"search-through-the-commands\">Search Through The Commands</h2><p>If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press <code>CTRL + R</code> and then type in the command. As you type you will see commands that match your search and when you find what you need, press <code>ENTER</code> and the command will run.</p><hr><h2 id=\"reading-files-in-parts\">Reading Files in Parts</h2><p>Use <code>less</code> to read a file a little by little. Many people use <code>cat</code> but if the file is big then using <code>cat</code> can be overwhelming. When you use <code>less</code> you can go through the file in parts and at your own pace. Example of usage:</p><pre><code class=\"language-Shell\">less test.txt</code></pre><p>To exit the file you are reading just type <code>q</code>.</p><hr><h2 id=\"and-there-s-still-many-more-\">And There's Still Many More!</h2><p>If you have any other linux tips that make your day less of a pain, please share them with everyone below!</p>","url":"https://backend.shahednasser.com/linux-tips-for-beginners/","canonical_url":null,"uuid":"1d697af0-765a-43f0-b8ab-f88b337dc65f","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5faa551f2280bc001e2ccd1f","reading_time":3,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently.</p><hr><h2 id=\"terminal-auto-completion\">Terminal Auto Completion</h2><p>This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the <code class=\"language-text\">TAB</code> key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are).</p><hr><h2 id=\"quick-navigation-between-directories\">Quick Navigation Between Directories</h2><p>As you know in Linux, to move from one directory to the other in the terminal you use the <code class=\"language-text\">cd</code> command. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token builtin class-name\">cd</span> my-folder</code></pre></div><p>There are three shortcuts that can help you navigate between directories:</p><ol><li><strong>Navigate to the Home directory</strong>: To move to the home directory, you just need to use <code class=\"language-text\">cd ~</code>.</li><li><strong>Navigate to the parent directory: </strong>To move to the parent directory, you just need to use <code class=\"language-text\">cd ..</code></li><li><strong>Navigate to the previous directory: </strong>To move to the directory you were previously in, you just need to use <code class=\"language-text\">cd -</code></li></ol><hr><h2 id=\"moving-to-the-beginning-and-end-of-line\">Moving To the Beginning and End of Line</h2><p>When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it.</p><ol><li><strong>To move to the beginning of the line</strong> press <code class=\"language-text\">CTRL + A</code></li><li><strong>To move to the end of the line </strong>press <code class=\"language-text\">CTRL + E</code></li></ol><hr><h2 id=\"read-a-file-as-it-updates\">Read a File As It Updates</h2><p>This is very essential when you are reading from a log file. To keep executing <code class=\"language-text\">tail</code> every time you want to check for changes can be a hassle and frankly eye blinding.</p><p><em>In case you're not aware, tail is a command used to read the end of a file.</em></p><p>So to read a file and have it update as any new lines are added to the file, just add <code class=\"language-text\">-f</code> option to the command. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">tail</span> -f /var/logs/apache2/error.log</code></pre></div><p>This can be used on any file as well, not just logs.</p><hr><h2 id=\"reuse-the-previous-parameter\">Reuse The Previous Parameter</h2><p>To use the parameter you've used in the previous command, use <code class=\"language-text\">!$</code>. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">touch</span> test.txt // creates a new <span class=\"token function\">file</span> test.txt\n<span class=\"token function\">nano</span> <span class=\"token operator\">!</span>$ // edit the previous parameter <span class=\"token function\">which</span> is test.txt</code></pre></div><hr><h2 id=\"reuse-the-previous-command\">Reuse The Previous Command</h2><p>To reuse the previous command, use <code class=\"language-text\">!!</code>. For example</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">tail</span> -n <span class=\"token number\">100</span> error.log\n<span class=\"token operator\">!</span><span class=\"token operator\">!</span> //use the previous <span class=\"token builtin class-name\">command</span> again</code></pre></div><p>This is mostly helpful when you run a command, but realize you need to add <code class=\"language-text\">sudo</code> to it. So instead of going to the beginning of the line to do that, just use <code class=\"language-text\">sudo !!</code></p><hr><h2 id=\"get-information-about-any-command\">Get Information About Any Command</h2><p>If you want to get information about any command like its description of the options and parameters it accepts, simply add <code class=\"language-text\">--help</code> to it. For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token builtin class-name\">cd</span> --help</code></pre></div><hr><h2 id=\"copying-and-pasting-from-the-terminal\">Copying and Pasting From The Terminal</h2><p>To copy anything from the terminal, use <code class=\"language-text\">CTRL + SHIFT + C</code></p><p>To paste anything to the terminal, use <code class=\"language-text\">CTRL + SHIFT + V</code></p><hr><h2 id=\"search-through-the-commands\">Search Through The Commands</h2><p>If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press <code class=\"language-text\">CTRL + R</code> and then type in the command. As you type you will see commands that match your search and when you find what you need, press <code class=\"language-text\">ENTER</code> and the command will run.</p><hr><h2 id=\"reading-files-in-parts\">Reading Files in Parts</h2><p>Use <code class=\"language-text\">less</code> to read a file a little by little. Many people use <code class=\"language-text\">cat</code> but if the file is big then using <code class=\"language-text\">cat</code> can be overwhelming. When you use <code class=\"language-text\">less</code> you can go through the file in parts and at your own pace. Example of usage:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">less</span> test.txt</code></pre></div><p>To exit the file you are reading just type <code class=\"language-text\">q</code>.</p><hr><h2 id=\"and-there-s-still-many-more-\">And There's Still Many More!</h2><p>If you have any other linux tips that make your day less of a pain, please share them with everyone below!</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many beginners find it hard to understand and adapt to Linux. In this post I will list a few helpful tips for Linux that can get you started on how to use it efficiently."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"terminal-auto-completion"},"children":[{"type":"text","value":"Terminal Auto Completion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is probably an easy, obvious one to many of us but it is still important to list. When using the terminal, you can use auto completion for commands or to use a file/folder in the directory you're currently in by pressing the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TAB"}]},{"type":"text","value":" key on your keyboard. This is helpful if you're still new to linux, if you just forgot what the name of the command is, or if you're just too lazy to type it out (like many of us are)."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"quick-navigation-between-directories"},"children":[{"type":"text","value":"Quick Navigation Between Directories"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you know in Linux, to move from one directory to the other in the terminal you use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" command. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" my-folder"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are three shortcuts that can help you navigate between directories:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the Home directory"}]},{"type":"text","value":": To move to the home directory, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd ~"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the parent directory: "}]},{"type":"text","value":"To move to the parent directory, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd .."}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Navigate to the previous directory: "}]},{"type":"text","value":"To move to the directory you were previously in, you just need to use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd -"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"moving-to-the-beginning-and-end-of-line"},"children":[{"type":"text","value":"Moving To the Beginning and End of Line"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you are running commands in the terminal, especially when you are copy/pasting the commands, a lot of time it can be a hassle when you want to edit something in the beginning of the line and then moving back to the end of it. There is an easier way to do it."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"To move to the beginning of the line"}]},{"type":"text","value":" press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + A"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"To move to the end of the line "}]},{"type":"text","value":"press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + E"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"read-a-file-as-it-updates"},"children":[{"type":"text","value":"Read a File As It Updates"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is very essential when you are reading from a log file. To keep executing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" every time you want to check for changes can be a hassle and frankly eye blinding."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"In case you're not aware, tail is a command used to read the end of a file."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So to read a file and have it update as any new lines are added to the file, just add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-f"}]},{"type":"text","value":" option to the command. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" -f /var/logs/apache2/error.log"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This can be used on any file as well, not just logs."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reuse-the-previous-parameter"},"children":[{"type":"text","value":"Reuse The Previous Parameter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To use the parameter you've used in the previous command, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!$"}]},{"type":"text","value":". For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"touch"}]},{"type":"text","value":" test.txt // creates a new "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"file"}]},{"type":"text","value":" test.txt\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nano"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"$ // edit the previous parameter "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"which"}]},{"type":"text","value":" is test.txt"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reuse-the-previous-command"},"children":[{"type":"text","value":"Reuse The Previous Command"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To reuse the previous command, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!!"}]},{"type":"text","value":". For example"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tail"}]},{"type":"text","value":" -n "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":" error.log\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":" //use the previous "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"command"}]},{"type":"text","value":" again"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is mostly helpful when you run a command, but realize you need to add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" to it. So instead of going to the beginning of the line to do that, just use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudo !!"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"get-information-about-any-command"},"children":[{"type":"text","value":"Get Information About Any Command"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to get information about any command like its description of the options and parameters it accepts, simply add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"--help"}]},{"type":"text","value":" to it. For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" --help"}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"copying-and-pasting-from-the-terminal"},"children":[{"type":"text","value":"Copying and Pasting From The Terminal"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To copy anything from the terminal, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + SHIFT + C"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To paste anything to the terminal, use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + SHIFT + V"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"search-through-the-commands"},"children":[{"type":"text","value":"Search Through The Commands"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you've used a command and you want to use it again, but you forgot some of it or you're just too lazy to type it out, one way to use it again is by searching in your terminal. To do that, press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CTRL + R"}]},{"type":"text","value":" and then type in the command. As you type you will see commands that match your search and when you find what you need, press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ENTER"}]},{"type":"text","value":" and the command will run."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"reading-files-in-parts"},"children":[{"type":"text","value":"Reading Files in Parts"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" to read a file a little by little. Many people use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" but if the file is big then using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" can be overwhelming. When you use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" you can go through the file in parts and at your own pace. Example of usage:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"shell"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"less"}]},{"type":"text","value":" test.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To exit the file you are reading just type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"q"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"and-there-s-still-many-more-"},"children":[{"type":"text","value":"And There's Still Many More!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have any other linux tips that make your day less of a pain, please share them with everyone below!"}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"terminal-auto-completion","heading":"Terminal Auto Completion"},{"id":"quick-navigation-between-directories","heading":"Quick Navigation Between Directories"},{"id":"moving-to-the-beginning-and-end-of-line","heading":"Moving To the Beginning and End of Line"},{"id":"read-a-file-as-it-updates","heading":"Read a File As It Updates"},{"id":"reuse-the-previous-parameter","heading":"Reuse The Previous Parameter"},{"id":"reuse-the-previous-command","heading":"Reuse The Previous Command"},{"id":"get-information-about-any-command","heading":"Get Information About Any Command"},{"id":"copying-and-pasting-from-the-terminal","heading":"Copying and Pasting From The Terminal"},{"id":"search-through-the-commands","heading":"Search Through The Commands"},{"id":"reading-files-in-parts","heading":"Reading Files in Parts"},{"id":"and-there-s-still-many-more-","heading":"And There's Still Many More!"}]},"featureImageSharp":{"base":"photo-1518432031352-d6fc5c10da5a.jpg","publicURL":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/photo-1518432031352-d6fc5c10da5a.jpg","imageMeta":{"width":2000,"height":1500},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAwAE/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAc2djoJaP//EABkQAQEBAAMAAAAAAAAAAAAAAAEAEQISIf/aAAgBAQABBQLuTZPPQ8lb/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGBAAAwEBAAAAAAAAAAAAAAAAABExARD/2gAIAQEABj8CnUh4U//EABoQAAMAAwEAAAAAAAAAAAAAAAABIRExQcH/2gAIAQEAAT8hn6QquhhfIHeUIZex/9oADAMBAAIAAwAAABAz/wD/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPxBH/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Qh//EABwQAQACAgMBAAAAAAAAAAAAAAEAESExQWGBsf/aAAgBAQABPxBQ0g84lVI3jiUOyZRCKywYoWmh+xjXN3P/2Q==","aspectRatio":1.3333333333333333,"src":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/d5c54/photo-1518432031352-d6fc5c10da5a.jpg","srcSet":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/65d8c/photo-1518432031352-d6fc5c10da5a.jpg 260w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/c5f21/photo-1518432031352-d6fc5c10da5a.jpg 520w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/d5c54/photo-1518432031352-d6fc5c10da5a.jpg 1040w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/81a53/photo-1518432031352-d6fc5c10da5a.jpg 1560w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/4e5f3/photo-1518432031352-d6fc5c10da5a.jpg 2000w","srcWebp":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/e4875/photo-1518432031352-d6fc5c10da5a.webp","srcSetWebp":"/static/cef95e4bbc4d4bc01a82516a264f8fe1/dc8f3/photo-1518432031352-d6fc5c10da5a.webp 260w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/2db4b/photo-1518432031352-d6fc5c10da5a.webp 520w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/e4875/photo-1518432031352-d6fc5c10da5a.webp 1040w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/f5845/photo-1518432031352-d6fc5c10da5a.webp 1560w,\n/static/cef95e4bbc4d4bc01a82516a264f8fe1/49d6b/photo-1518432031352-d6fc5c10da5a.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"prev":{"id":"Ghost__Post__6127ba1b3ed159214d382e74","title":"How I Learned About Contributing to Open Source Projects By Creating One","slug":"how-i-learned-about-contributing-to-open-source-projects-by-creating-one","featured":false,"feature_image":"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1579389083046-e3df9c2b3325.jpg","excerpt":"Every beginner can agree that your first contribution is very scary. This is how I learned to contribute to open source projects by creating one.","custom_excerpt":"Every beginner can agree that your first contribution is very scary. This is how I learned to contribute to open source projects by creating one.","visibility":"public","created_at_pretty":"12 Nov 2020","published_at_pretty":"12 Nov 2020","updated_at_pretty":"26 Aug 2021","created_at":"2020-11-12T08:46:22.000+00:00","published_at":"2020-11-12T10:54:17.000+00:00","updated_at":"2021-08-26T17:53:14.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"hacktoberfest","url":"https://backend.shahednasser.com/tag/hacktoberfest/","name":"Hacktoberfest","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},"tags":[{"slug":"hacktoberfest","url":"https://backend.shahednasser.com/tag/hacktoberfest/","name":"Hacktoberfest","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"my-experience","url":"https://backend.shahednasser.com/tag/my-experience/","name":"My Experience","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1493612276216-ee3925520721-2-1.jpeg","description":"Sharing my experience in or opinions regarding programming, work, or other concepts.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"tips","url":"https://backend.shahednasser.com/tag/tips/","name":"Tips","visibility":"public","feature_image":null,"description":"Learn more about programming and development through these articles that have essential tips!","meta_title":"Tips on Technology and Programming","meta_description":null,"featureImageSharp":null},{"slug":"projects","url":"https://backend.shahednasser.com/tag/projects/","name":"Projects","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null}],"plaintext":"This article was mentioned on Digital Ocean's Hacktoberfest Recap\n[https://www.digitalocean.com/blog/hacktoberfest-recap2020].\n\nEvery beginner, whether to programming or to Open Source Projects, can agree\nthat your first contribution is very scary. Not only do you not know how it\nworks, but also you're scared of what response you will get when you send that\nfirst PR.\n\nA lot of projects on GitHub aim to make first time contributors less scared of\nthis. For example, the repository First Contributions\n[https://github.com/firstcontributions/first-contributions] aim to help you make\nthat first contribution you are so scared of by making a simple change to the\nrepository and guiding you in details on how to submit the PR. This is great and\nreally helpful, but when you go to make a contribution to a repository that\nrequires more real work, you still find it a little hard to contribute to it.\n\nI personally had a trouble with it. I'm not a beginner developer, but I am a\nbeginner to Open Source and trying those \"repositories for first timers\" were\nhelpful in taking that first step, but I still found it difficult to contribute\nto bigger repositories. This was a hindrance for me as I really wanted to be a\npart of the Open Source community and collaborating with other contributors.\n\nThen one day, I decided to create a repository that was so simple. The idea was\njust creating a library of pure CSS buttons that can be used in any website. I\nadmit it's not an innovative idea, but it was a way to help me start with open\nsource projects. The repository I created is sButtons\n[https://github.com/sButtons/sbuttons].\n\nThe website in the beginning was very simple and had almost nothing. It just had\nat most 15 buttons, a minimalist sidebar, and a text for a logo. It didn't even\nhave a header or a footer, and the buttons were not that special, to be frank.\n\nThen I started posting issues to get help on making the library and the website\nthat showcases it better. I posted issues about adding new buttons, improving\nthe design and look and feel of the sidebar and navigation bar, adding a header\nand a footer, and even creating a logo, among other issues. At first I expected\nno one to care, as it was a small repository and maybe not that interesting, but\nI was overwhelmed by the comments and helps I received! People wanted to\ncontribute to this open source project even though it had nothing in it.\n\nSince I am a beginner to open source, I decided that this repository would\nmainly be focused on other beginner contributors. The main values to this\nrepository would be:\n\n 1. Try to always add simple issues that any beginner can take, and make sure no\n    one other than beginners work on it.\n 2. When reviewing PRs or replying to other, make sure to never make beginners\n    feel bad for their questions. Guide them through everything in steps, and be\n    patient.\n 3. Be welcoming of everyone.\n\nAnd the number of contributors grew to maybe 60 which was a big deal to me! And\nthen Hacktoberfest [https://hacktoberfest.digitalocean.com/] started, and for\nthose of you who don't know, it's an annual event for developers that encourage\ncontributing to open source projects by offering swag as a prize for your\ncontributions. Developers all around the world participate and it is a big event\nfor everyone.\n\nI've never been a maintainer through Hacktoberfest. This was my first time, and\nas October started (which is the month Hacktoberfest happens in), I was\noverwhelmed by the contributors that wanted to contribute to the repository to\nparticipate in Hacktoberfest. I got into a point where I couldn't keep up with\nall the notifications, the issues and comments and PRs, so I created an\norganization, transferred the repository to it and added amazing members that\nstill help massively in responding and reviewing and guiding beginner\ncontributors. Without these members, my repository would still be in stage one.\n\nLong story short, now (at the time of posting this) the repository has 113 stars\nand 420 forks, 180 contributors\n[https://github.com/sButtons/sbuttons/blob/master/CONTRIBUTORS.md], an NPM\npackage [https://www.npmjs.com/package/sbuttons], and an awesome new renovated\nwebsite. [https://sbuttons.github.io/sbuttons/]\n\nWe still have a long way to go, but I am very proud of what we have accomplished\nas we are all beginners and we are aspiring to help other beginners. \n\nThrough creating this project and repository, it helped me learn more about\ncontributing to open source and collaborating with developers all over the\nworld. Through helping others it helped me learn new things, and I think that's\nthe beauty and purpose of open source projects. Helping and being part of the\ncommunity.\n\n> The beauty and purpose of open source projects is helping and being part of the\ncommunity.\nIf you are new to open source projects as well or you would like to be a\ncontributor to our repository, we have many issues waiting for you! Please check\nthem out and start contributing here\n[https://github.com/sButtons/sbuttons/issues]!\n\nIn the end, I would like to mention all the members of the organization who have\nbeen a great help for me and who have taught me many things as well. They are\n(in no particular order):\n\n 1. Gleb Kemarsky [https://github.com/glebkema]\n 2. Sonny Michael [https://github.com/isonnymichael]\n 3. Chaitanya Chaturvedi [https://github.com/chaitanya4vedi]\n 4. Seven [https://github.com/Caleb335]\n 5. Aditya Vats [https://github.com/HADES-01]\n 6. Snehasish Dhar [https://github.com/dsnehasish74]\n 7. SARVESHKHANDELWAL [https://github.com/SARVESHKHANDELWAL]\n 8. Tom Wang [https://github.com/TomWBush]\n\nThank you all and thank you to every contributors that contributed to our\nproject!","html":"<p><em>This article was mentioned on Digital Ocean's <a href=\"https://www.digitalocean.com/blog/hacktoberfest-recap2020\">Hacktoberfest Recap</a>.</em></p><p>Every beginner, whether to programming or to Open Source Projects, can agree that your first contribution is very scary. Not only do you not know how it works, but also you're scared of what response you will get when you send that first PR.</p><p>A lot of projects on GitHub aim to make first time contributors less scared of this. For example, the repository <a href=\"https://github.com/firstcontributions/first-contributions\">First Contributions</a> aim to help you make that first contribution you are so scared of by making a simple change to the repository and guiding you in details on how to submit the PR. This is great and really helpful, but when you go to make a contribution to a repository that requires more real work, you still find it a little hard to contribute to it.</p><p>I personally had a trouble with it. I'm not a beginner developer, but I am a beginner to Open Source and trying those \"repositories for first timers\" were helpful in taking that first step, but I still found it difficult to contribute to bigger repositories. This was a hindrance for me as I really wanted to be a part of the Open Source community and collaborating with other contributors.</p><p>Then one day, I decided to create a repository that was so simple. The idea was just creating a library of pure CSS buttons that can be used in any website. I admit it's not an innovative idea, but it was a way to help me start with open source projects. The repository I created is <a href=\"https://github.com/sButtons/sbuttons\"><strong>sButtons</strong></a>.</p><p>The website in the beginning was very simple and had almost nothing. It just had at most 15 buttons, a minimalist sidebar, and a text for a logo. It didn't even have a header or a footer, and the buttons were not that special, to be frank.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/94340611-6dc3aa00-002d-11eb-8658-1e7a005e34d2.gif\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>Then I started posting issues to get help on making the library and the website that showcases it better. I posted issues about adding new buttons, improving the design and look and feel of the sidebar and navigation bar, adding a header and a footer, and even creating a logo, among other issues. At first I expected no one to care, as it was a small repository and maybe not that interesting, but I was overwhelmed by the comments and helps I received! People wanted to contribute to this open source project even though it had nothing in it.</p><p>Since I am a beginner to open source, I decided that this repository would mainly be focused on other beginner contributors. The main values to this repository would be:</p><ol><li>Try to always add simple issues that any beginner can take, and make sure no one other than beginners work on it.</li><li>When reviewing PRs or replying to other, make sure to never make beginners feel bad for their questions. Guide them through everything in steps, and be patient.</li><li>Be welcoming of everyone.</li></ol><p>And the number of contributors grew to maybe 60 which was a big deal to me! And then <a href=\"https://hacktoberfest.digitalocean.com/\">Hacktoberfest</a> started, and for those of you who don't know, it's an annual event for developers that encourage contributing to open source projects by offering swag as a prize for your contributions. Developers all around the world participate and it is a big event for everyone.</p><p>I've never been a maintainer through Hacktoberfest. This was my first time, and as October started (which is the month Hacktoberfest happens in), I was overwhelmed by the contributors that wanted to contribute to the repository to participate in Hacktoberfest. I got into a point where I couldn't keep up with all the notifications, the issues and comments and PRs, so I created an organization, transferred the repository to it and added amazing members that still help massively in responding and reviewing and guiding beginner contributors. Without these members, my repository would still be in stage one.</p><p>Long story short, now (at the time of posting this) the repository has 113 stars and 420 forks, <a href=\"https://github.com/sButtons/sbuttons/blob/master/CONTRIBUTORS.md\">180 contributors</a>, an <a href=\"https://www.npmjs.com/package/sbuttons\">NPM package</a>, and <a href=\"https://sbuttons.github.io/sbuttons/\">an awesome new renovated website.</a></p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/Screenshot-from-2020-11-12-11-26-20.png\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>We still have a long way to go, but I am very proud of what we have accomplished as we are all beginners and we are aspiring to help other beginners. </p><p>Through creating this project and repository, it helped me learn more about contributing to open source and collaborating with developers all over the world. Through helping others it helped me learn new things, and I think that's the beauty and purpose of open source projects. <strong>Helping and being part of the community.</strong></p><blockquote>The beauty and purpose of open source projects is helping and being part of the community.</blockquote><p>If you are new to open source projects as well or you would like to be a contributor to our repository, we have many issues waiting for you! Please check them out and start contributing <a href=\"https://github.com/sButtons/sbuttons/issues\">here</a>!</p><p>In the end, I would like to mention all the members of the organization who have been a great help for me and who have taught me many things as well. They are (in no particular order):</p><ol><li><a href=\"https://github.com/glebkema\">Gleb Kemarsky</a></li><li><a href=\"https://github.com/isonnymichael\">Sonny Michael</a></li><li><a href=\"https://github.com/chaitanya4vedi\">Chaitanya Chaturvedi</a></li><li><a href=\"https://github.com/Caleb335\">Seven</a></li><li><a href=\"https://github.com/HADES-01\">Aditya Vats</a></li><li><a href=\"https://github.com/dsnehasish74\">Snehasish Dhar</a></li><li><a href=\"https://github.com/SARVESHKHANDELWAL\">SARVESHKHANDELWAL</a></li><li><a href=\"https://github.com/TomWBush\">Tom Wang</a></li></ol><p>Thank you all and thank you to every contributors that contributed to our project!</p>","url":"https://backend.shahednasser.com/how-i-learned-about-contributing-to-open-source-projects-by-creating-one/","canonical_url":null,"uuid":"7cb596df-2ba0-43f6-90ea-071d7d24644b","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5facf65ea093c6001e9002fd","reading_time":4,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p><em>This article was mentioned on Digital Ocean's <a href=\"https://www.digitalocean.com/blog/hacktoberfest-recap2020\">Hacktoberfest Recap</a>.</em></p><p>Every beginner, whether to programming or to Open Source Projects, can agree that your first contribution is very scary. Not only do you not know how it works, but also you're scared of what response you will get when you send that first PR.</p><p>A lot of projects on GitHub aim to make first time contributors less scared of this. For example, the repository <a href=\"https://github.com/firstcontributions/first-contributions\">First Contributions</a> aim to help you make that first contribution you are so scared of by making a simple change to the repository and guiding you in details on how to submit the PR. This is great and really helpful, but when you go to make a contribution to a repository that requires more real work, you still find it a little hard to contribute to it.</p><p>I personally had a trouble with it. I'm not a beginner developer, but I am a beginner to Open Source and trying those \"repositories for first timers\" were helpful in taking that first step, but I still found it difficult to contribute to bigger repositories. This was a hindrance for me as I really wanted to be a part of the Open Source community and collaborating with other contributors.</p><p>Then one day, I decided to create a repository that was so simple. The idea was just creating a library of pure CSS buttons that can be used in any website. I admit it's not an innovative idea, but it was a way to help me start with open source projects. The repository I created is <a href=\"https://github.com/sButtons/sbuttons\"><strong>sButtons</strong></a>.</p><p>The website in the beginning was very simple and had almost nothing. It just had at most 15 buttons, a minimalist sidebar, and a text for a logo. It didn't even have a header or a footer, and the buttons were not that special, to be frank.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/94340611-6dc3aa00-002d-11eb-8658-1e7a005e34d2.gif\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>Then I started posting issues to get help on making the library and the website that showcases it better. I posted issues about adding new buttons, improving the design and look and feel of the sidebar and navigation bar, adding a header and a footer, and even creating a logo, among other issues. At first I expected no one to care, as it was a small repository and maybe not that interesting, but I was overwhelmed by the comments and helps I received! People wanted to contribute to this open source project even though it had nothing in it.</p><p>Since I am a beginner to open source, I decided that this repository would mainly be focused on other beginner contributors. The main values to this repository would be:</p><ol><li>Try to always add simple issues that any beginner can take, and make sure no one other than beginners work on it.</li><li>When reviewing PRs or replying to other, make sure to never make beginners feel bad for their questions. Guide them through everything in steps, and be patient.</li><li>Be welcoming of everyone.</li></ol><p>And the number of contributors grew to maybe 60 which was a big deal to me! And then <a href=\"https://hacktoberfest.digitalocean.com/\">Hacktoberfest</a> started, and for those of you who don't know, it's an annual event for developers that encourage contributing to open source projects by offering swag as a prize for your contributions. Developers all around the world participate and it is a big event for everyone.</p><p>I've never been a maintainer through Hacktoberfest. This was my first time, and as October started (which is the month Hacktoberfest happens in), I was overwhelmed by the contributors that wanted to contribute to the repository to participate in Hacktoberfest. I got into a point where I couldn't keep up with all the notifications, the issues and comments and PRs, so I created an organization, transferred the repository to it and added amazing members that still help massively in responding and reviewing and guiding beginner contributors. Without these members, my repository would still be in stage one.</p><p>Long story short, now (at the time of posting this) the repository has 113 stars and 420 forks, <a href=\"https://github.com/sButtons/sbuttons/blob/master/CONTRIBUTORS.md\">180 contributors</a>, an <a href=\"https://www.npmjs.com/package/sbuttons\">NPM package</a>, and <a href=\"https://sbuttons.github.io/sbuttons/\">an awesome new renovated website.</a></p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/Screenshot-from-2020-11-12-11-26-20.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>We still have a long way to go, but I am very proud of what we have accomplished as we are all beginners and we are aspiring to help other beginners. </p><p>Through creating this project and repository, it helped me learn more about contributing to open source and collaborating with developers all over the world. Through helping others it helped me learn new things, and I think that's the beauty and purpose of open source projects. <strong>Helping and being part of the community.</strong></p><blockquote>The beauty and purpose of open source projects is helping and being part of the community.</blockquote><p>If you are new to open source projects as well or you would like to be a contributor to our repository, we have many issues waiting for you! Please check them out and start contributing <a href=\"https://github.com/sButtons/sbuttons/issues\">here</a>!</p><p>In the end, I would like to mention all the members of the organization who have been a great help for me and who have taught me many things as well. They are (in no particular order):</p><ol><li><a href=\"https://github.com/glebkema\">Gleb Kemarsky</a></li><li><a href=\"https://github.com/isonnymichael\">Sonny Michael</a></li><li><a href=\"https://github.com/chaitanya4vedi\">Chaitanya Chaturvedi</a></li><li><a href=\"https://github.com/Caleb335\">Seven</a></li><li><a href=\"https://github.com/HADES-01\">Aditya Vats</a></li><li><a href=\"https://github.com/dsnehasish74\">Snehasish Dhar</a></li><li><a href=\"https://github.com/SARVESHKHANDELWAL\">SARVESHKHANDELWAL</a></li><li><a href=\"https://github.com/TomWBush\">Tom Wang</a></li></ol><p>Thank you all and thank you to every contributors that contributed to our project!</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"This article was mentioned on Digital Ocean's "},{"type":"element","tagName":"a","properties":{"href":"https://www.digitalocean.com/blog/hacktoberfest-recap2020"},"children":[{"type":"text","value":"Hacktoberfest Recap"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Every beginner, whether to programming or to Open Source Projects, can agree that your first contribution is very scary. Not only do you not know how it works, but also you're scared of what response you will get when you send that first PR."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A lot of projects on GitHub aim to make first time contributors less scared of this. For example, the repository "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/firstcontributions/first-contributions"},"children":[{"type":"text","value":"First Contributions"}]},{"type":"text","value":" aim to help you make that first contribution you are so scared of by making a simple change to the repository and guiding you in details on how to submit the PR. This is great and really helpful, but when you go to make a contribution to a repository that requires more real work, you still find it a little hard to contribute to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I personally had a trouble with it. I'm not a beginner developer, but I am a beginner to Open Source and trying those \"repositories for first timers\" were helpful in taking that first step, but I still found it difficult to contribute to bigger repositories. This was a hindrance for me as I really wanted to be a part of the Open Source community and collaborating with other contributors."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then one day, I decided to create a repository that was so simple. The idea was just creating a library of pure CSS buttons that can be used in any website. I admit it's not an innovative idea, but it was a way to help me start with open source projects. The repository I created is "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/sButtons/sbuttons"},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sButtons"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The website in the beginning was very simple and had almost nothing. It just had at most 15 buttons, a minimalist sidebar, and a text for a logo. It didn't even have a header or a footer, and the buttons were not that special, to be frank."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/94340611-6dc3aa00-002d-11eb-8658-1e7a005e34d2.gif","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then I started posting issues to get help on making the library and the website that showcases it better. I posted issues about adding new buttons, improving the design and look and feel of the sidebar and navigation bar, adding a header and a footer, and even creating a logo, among other issues. At first I expected no one to care, as it was a small repository and maybe not that interesting, but I was overwhelmed by the comments and helps I received! People wanted to contribute to this open source project even though it had nothing in it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since I am a beginner to open source, I decided that this repository would mainly be focused on other beginner contributors. The main values to this repository would be:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Try to always add simple issues that any beginner can take, and make sure no one other than beginners work on it."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"When reviewing PRs or replying to other, make sure to never make beginners feel bad for their questions. Guide them through everything in steps, and be patient."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Be welcoming of everyone."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And the number of contributors grew to maybe 60 which was a big deal to me! And then "},{"type":"element","tagName":"a","properties":{"href":"https://hacktoberfest.digitalocean.com/"},"children":[{"type":"text","value":"Hacktoberfest"}]},{"type":"text","value":" started, and for those of you who don't know, it's an annual event for developers that encourage contributing to open source projects by offering swag as a prize for your contributions. Developers all around the world participate and it is a big event for everyone."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I've never been a maintainer through Hacktoberfest. This was my first time, and as October started (which is the month Hacktoberfest happens in), I was overwhelmed by the contributors that wanted to contribute to the repository to participate in Hacktoberfest. I got into a point where I couldn't keep up with all the notifications, the issues and comments and PRs, so I created an organization, transferred the repository to it and added amazing members that still help massively in responding and reviewing and guiding beginner contributors. Without these members, my repository would still be in stage one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Long story short, now (at the time of posting this) the repository has 113 stars and 420 forks, "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/sButtons/sbuttons/blob/master/CONTRIBUTORS.md"},"children":[{"type":"text","value":"180 contributors"}]},{"type":"text","value":", an "},{"type":"element","tagName":"a","properties":{"href":"https://www.npmjs.com/package/sbuttons"},"children":[{"type":"text","value":"NPM package"}]},{"type":"text","value":", and "},{"type":"element","tagName":"a","properties":{"href":"https://sbuttons.github.io/sbuttons/"},"children":[{"type":"text","value":"an awesome new renovated website."}]}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/Screenshot-from-2020-11-12-11-26-20.png","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We still have a long way to go, but I am very proud of what we have accomplished as we are all beginners and we are aspiring to help other beginners. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Through creating this project and repository, it helped me learn more about contributing to open source and collaborating with developers all over the world. Through helping others it helped me learn new things, and I think that's the beauty and purpose of open source projects. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Helping and being part of the community."}]}]},{"type":"element","tagName":"blockquote","properties":{},"children":[{"type":"text","value":"The beauty and purpose of open source projects is helping and being part of the community."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are new to open source projects as well or you would like to be a contributor to our repository, we have many issues waiting for you! Please check them out and start contributing "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/sButtons/sbuttons/issues"},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the end, I would like to mention all the members of the organization who have been a great help for me and who have taught me many things as well. They are (in no particular order):"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/glebkema"},"children":[{"type":"text","value":"Gleb Kemarsky"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/isonnymichael"},"children":[{"type":"text","value":"Sonny Michael"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/chaitanya4vedi"},"children":[{"type":"text","value":"Chaitanya Chaturvedi"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/Caleb335"},"children":[{"type":"text","value":"Seven"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/HADES-01"},"children":[{"type":"text","value":"Aditya Vats"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/dsnehasish74"},"children":[{"type":"text","value":"Snehasish Dhar"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/SARVESHKHANDELWAL"},"children":[{"type":"text","value":"SARVESHKHANDELWAL"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/TomWBush"},"children":[{"type":"text","value":"Tom Wang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thank you all and thank you to every contributors that contributed to our project!"}]}],"data":{"quirksMode":false}},"tableOfContents":[]},"featureImageSharp":{"base":"photo-1579389083046-e3df9c2b3325.jpg","publicURL":"/static/cb44950c4ed892aa5bdb959335cb60aa/photo-1579389083046-e3df9c2b3325.jpg","imageMeta":{"width":2000,"height":3000},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAeABQDASIAAhEBAxEB/8QAGQAAAwADAAAAAAAAAAAAAAAAAAQFAgMG/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQIA/9oADAMBAAIQAxAAAAFZaoqZQ3A0pVzm6lgzJf/EAB0QAAIBBAMAAAAAAAAAAAAAAAECAwAEERITITL/2gAIAQEAAQUCmgzI1s60igrMuS0OwWJCJh1zurG5NP5IyU01/8QAFhEAAwAAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/ASv/xAAWEQADAAAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8BI//EAB4QAAIBAwUAAAAAAAAAAAAAAAABERAhMRIyQ2GB/9oACAEBAAY/Am5MqC4nNkTrOR+UzKMUfRtP/8QAHRAAAwACAgMAAAAAAAAAAAAAAAERIUFRYTGR0f/aAAgBAQABPyFuCERugukVsyWxtmThZc6KhPsUGv7FoNjxBh/Yqt9FDoJAf//aAAwDAQACAAMAAAAQSPRO/8QAFxEBAQEBAAAAAAAAAAAAAAAAAAERUf/aAAgBAwEBPxCMcRj/xAAXEQEBAQEAAAAAAAAAAAAAAAABABFR/9oACAECAQE/EGV2bS//xAAeEAEBAQACAgMBAAAAAAAAAAABEQAhQTFRYXGB8f/aAAgBAQABPxCEFtVgHxjHNYjyvqe8wWC4OTGfPLInrIhQ1ls9P8039eufq4ltE6KTCBixQpu2P2aZHbhyqM5kPLhQ8i87/9k=","aspectRatio":0.6653992395437263,"src":"/static/cb44950c4ed892aa5bdb959335cb60aa/ea4ab/photo-1579389083046-e3df9c2b3325.jpg","srcSet":"/static/cb44950c4ed892aa5bdb959335cb60aa/477ba/photo-1579389083046-e3df9c2b3325.jpg 175w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/06776/photo-1579389083046-e3df9c2b3325.jpg 350w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/ea4ab/photo-1579389083046-e3df9c2b3325.jpg 700w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/3055e/photo-1579389083046-e3df9c2b3325.jpg 1050w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/eff08/photo-1579389083046-e3df9c2b3325.jpg 1400w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/4e5f3/photo-1579389083046-e3df9c2b3325.jpg 2000w","srcWebp":"/static/cb44950c4ed892aa5bdb959335cb60aa/89afa/photo-1579389083046-e3df9c2b3325.webp","srcSetWebp":"/static/cb44950c4ed892aa5bdb959335cb60aa/9fca7/photo-1579389083046-e3df9c2b3325.webp 175w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/37a4e/photo-1579389083046-e3df9c2b3325.webp 350w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/89afa/photo-1579389083046-e3df9c2b3325.webp 700w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/78e7a/photo-1579389083046-e3df9c2b3325.webp 1050w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/03d34/photo-1579389083046-e3df9c2b3325.webp 1400w,\n/static/cb44950c4ed892aa5bdb959335cb60aa/49d6b/photo-1579389083046-e3df9c2b3325.webp 2000w","sizes":"(max-width: 700px) 100vw, 700px"}}}},"next":{"id":"Ghost__Post__6127ba1b3ed159214d382e6e","title":"Web Design Tips For Web Developers","slug":"web-design-tips-for-web-developers","featured":false,"feature_image":"https://res-1.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1542744094-3a31f272c490.jpg","excerpt":"Some tips on Web Design for Web Developers I have learned from a FREE Udemy course that are essential for your websites.","custom_excerpt":"Some tips on Web Design for Web Developers I have learned from a FREE Udemy course that are essential for your websites.","visibility":"public","created_at_pretty":"26 Oct 2020","published_at_pretty":"26 Oct 2020","updated_at_pretty":"26 Aug 2021","created_at":"2020-10-26T21:36:54.000+00:00","published_at":"2020-10-26T21:41:21.000+00:00","updated_at":"2021-08-26T17:53:42.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"design","url":"https://backend.shahednasser.com/tag/design/","name":"Design","visibility":"public","feature_image":null,"description":"Learn more about Web Design through tutorials, articles and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},"tags":[{"slug":"design","url":"https://backend.shahednasser.com/tag/design/","name":"Design","visibility":"public","feature_image":null,"description":"Learn more about Web Design through tutorials, articles and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"tips","url":"https://backend.shahednasser.com/tag/tips/","name":"Tips","visibility":"public","feature_image":null,"description":"Learn more about programming and development through these articles that have essential tips!","meta_title":"Tips on Technology and Programming","meta_description":null,"featureImageSharp":null},{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null}],"plaintext":"Recently I took a course on Udemy called Web Design for Web Developers\n[https://www.udemy.com/web-design-secrets/learn/v4/overview]. The course is very\nshort and has a few simple tips that are actually really important to achieve a\nclean design for your website.\n\nI advise you to take the course - it is only one hour long and it is FREE - to\nget a better understanding of these tips. However, I will be sharing with you\nwhat I learned in the hopes that it will help you out.\n\n\n--------------------------------------------------------------------------------\n\nTypography\n * Font size should be between 15px and 25px for body text.\n * Headlines should have a big font size\n * Line height should be between 120% and 150% of font size.\n * Pick a font that reflects the feel of the website, and stick with it for the\n   entire website\n\n\n--------------------------------------------------------------------------------\n\nColors\n * Specify a base color\n * Using online tools, create a palette based on different shades of the base\n   color or what colors go well with it.\n * For Call To Action buttons, use colors that will draw the attention of the\n   user\n * Do not overuse Black in your design\n\n\n--------------------------------------------------------------------------------\n\nImages\n * When putting text over images, make sure to add an overlay to the image so\n   the text can be readable.\n * If you do not want to add an overlay, you could put the text in a box and\n   give it a background color.\n * Another option is to blur the image.\n\n\n--------------------------------------------------------------------------------\n\nIcons\n * Use icons for features, services, steps, etc.\n * Make sure the icons are easily understood to not cause confusion, especially\n   if they are not accompanied by text.\n * Label your icons.\n\n\n--------------------------------------------------------------------------------\n\nSpacing/Layout\n * Put whitespace between website sections to make the start and end of a\n   section apparent.\n * Base the layout on what you want your audience to focus on.\n * Make sure the whitespace between elements is not too small that everything\n   looks crowded\n\n\n--------------------------------------------------------------------------------\n\nConclusion\nThese are just a few of the helpful tips I learned from the course. To get more\ninsight and understand the concepts more clearly, take an hour to go through the\ncourse.\n\nSuggested Read\nHiring managers looking to spot the best web developers may want to check out\nthis article [https://www.toptal.com/web#hiring-guide] from Toptal.","html":"<p>Recently I took a course on Udemy called <a href=\"https://www.udemy.com/web-design-secrets/learn/v4/overview\" rel=\"noopener noreferrer\">Web Design for Web Developers</a>. The course is very short and has a few simple tips that are actually really important to achieve a clean design for your website.</p><p>I advise you to take the course - it is only one hour long and it is <strong>FREE</strong> - to get a better understanding of these tips. However, I will be sharing with you what I learned in the hopes that it will help you out.</p><hr><h3 id=\"typography\">Typography</h3><ul><li>Font size should be between 15px and 25px for body text.</li><li>Headlines should have a big font size</li><li>Line height should be between 120% and 150% of font size.</li><li>Pick a font that reflects the feel of the website, and stick with it for the entire website</li></ul><hr><h3 id=\"colors\">Colors</h3><ul><li>Specify a base color</li><li>Using online tools, create a palette based on different shades of the base color or what colors go well with it.</li><li>For Call To Action buttons, use colors that will draw the attention of the user</li><li>Do not overuse Black in your design</li></ul><hr><h3 id=\"images\">Images</h3><ul><li>When putting text over images, make sure to add an overlay to the image so the text can be readable.</li><li>If you do not want to add an overlay, you could put the text in a box and give it a background color.</li><li>Another option is to blur the image.</li></ul><hr><h3 id=\"icons\">Icons</h3><ul><li>Use icons for features, services, steps, etc.</li><li>Make sure the icons are easily understood to not cause confusion, especially if they are not accompanied by text.</li><li>Label your icons.</li></ul><hr><h3 id=\"spacing-layout\">Spacing/Layout</h3><ul><li>Put whitespace between website sections to make the start and end of a section apparent.</li><li>Base the layout on what you want your audience to focus on.</li><li>Make sure the whitespace between elements is not too small that everything looks crowded</li></ul><hr><h3 id=\"conclusion\">Conclusion</h3><p>These are just a few of the helpful tips I learned from the course. To get more insight and understand the concepts more clearly, take an hour to go through the course.</p><!--kg-card-begin: html--><div class=\"suggested-read\"><h4>Suggested Read</h4><p>Hiring managers looking to spot the best web developers may want <a href=\"https://www.toptal.com/web#hiring-guide\" rel=\"noopener noreferrer\" target=\"_blank\">to check out this article</a> from Toptal. </p></div><!--kg-card-end: html-->","url":"https://backend.shahednasser.com/web-design-tips-for-web-developers/","canonical_url":null,"uuid":"ef3613d0-0c45-4d78-ab57-ca1e06b77528","codeinjection_foot":null,"codeinjection_head":"<style>\n    .suggested-read {\n    \tbackground-color: #e8e8e8;\n    \tpadding: 20px;\n\t}\n</style>","codeinjection_styles":"\n    .suggested-read {\n    \tbackground-color: #e8e8e8;\n    \tpadding: 20px;\n\t}\n","comment_id":"5f974176b366d4001e86cc9e","reading_time":1,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Recently I took a course on Udemy called <a href=\"https://www.udemy.com/web-design-secrets/learn/v4/overview\" rel=\"noopener noreferrer\">Web Design for Web Developers</a>. The course is very short and has a few simple tips that are actually really important to achieve a clean design for your website.</p><p>I advise you to take the course - it is only one hour long and it is <strong>FREE</strong> - to get a better understanding of these tips. However, I will be sharing with you what I learned in the hopes that it will help you out.</p><hr><h3 id=\"typography\">Typography</h3><ul><li>Font size should be between 15px and 25px for body text.</li><li>Headlines should have a big font size</li><li>Line height should be between 120% and 150% of font size.</li><li>Pick a font that reflects the feel of the website, and stick with it for the entire website</li></ul><hr><h3 id=\"colors\">Colors</h3><ul><li>Specify a base color</li><li>Using online tools, create a palette based on different shades of the base color or what colors go well with it.</li><li>For Call To Action buttons, use colors that will draw the attention of the user</li><li>Do not overuse Black in your design</li></ul><hr><h3 id=\"images\">Images</h3><ul><li>When putting text over images, make sure to add an overlay to the image so the text can be readable.</li><li>If you do not want to add an overlay, you could put the text in a box and give it a background color.</li><li>Another option is to blur the image.</li></ul><hr><h3 id=\"icons\">Icons</h3><ul><li>Use icons for features, services, steps, etc.</li><li>Make sure the icons are easily understood to not cause confusion, especially if they are not accompanied by text.</li><li>Label your icons.</li></ul><hr><h3 id=\"spacing-layout\">Spacing/Layout</h3><ul><li>Put whitespace between website sections to make the start and end of a section apparent.</li><li>Base the layout on what you want your audience to focus on.</li><li>Make sure the whitespace between elements is not too small that everything looks crowded</li></ul><hr><h3 id=\"conclusion\">Conclusion</h3><p>These are just a few of the helpful tips I learned from the course. To get more insight and understand the concepts more clearly, take an hour to go through the course.</p><!--kg-card-begin: html--><div class=\"suggested-read\"><h4>Suggested Read</h4><p>Hiring managers looking to spot the best web developers may want <a href=\"https://www.toptal.com/web#hiring-guide\" rel=\"noopener noreferrer\" target=\"_blank\">to check out this article</a> from Toptal. </p></div><!--kg-card-end: html-->","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Recently I took a course on Udemy called "},{"type":"element","tagName":"a","properties":{"href":"https://www.udemy.com/web-design-secrets/learn/v4/overview","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Web Design for Web Developers"}]},{"type":"text","value":". The course is very short and has a few simple tips that are actually really important to achieve a clean design for your website."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I advise you to take the course - it is only one hour long and it is "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"FREE"}]},{"type":"text","value":" - to get a better understanding of these tips. However, I will be sharing with you what I learned in the hopes that it will help you out."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"typography"},"children":[{"type":"text","value":"Typography"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Font size should be between 15px and 25px for body text."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Headlines should have a big font size"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Line height should be between 120% and 150% of font size."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Pick a font that reflects the feel of the website, and stick with it for the entire website"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"colors"},"children":[{"type":"text","value":"Colors"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Specify a base color"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Using online tools, create a palette based on different shades of the base color or what colors go well with it."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"For Call To Action buttons, use colors that will draw the attention of the user"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Do not overuse Black in your design"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"images"},"children":[{"type":"text","value":"Images"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"When putting text over images, make sure to add an overlay to the image so the text can be readable."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If you do not want to add an overlay, you could put the text in a box and give it a background color."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Another option is to blur the image."}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"icons"},"children":[{"type":"text","value":"Icons"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Use icons for features, services, steps, etc."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Make sure the icons are easily understood to not cause confusion, especially if they are not accompanied by text."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Label your icons."}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"spacing-layout"},"children":[{"type":"text","value":"Spacing/Layout"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Put whitespace between website sections to make the start and end of a section apparent."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Base the layout on what you want your audience to focus on."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Make sure the whitespace between elements is not too small that everything looks crowded"}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h3","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These are just a few of the helpful tips I learned from the course. To get more insight and understand the concepts more clearly, take an hour to go through the course."}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"div","properties":{"className":["suggested-read"]},"children":[{"type":"element","tagName":"h4","properties":{},"children":[{"type":"text","value":"Suggested Read"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hiring managers looking to spot the best web developers may want "},{"type":"element","tagName":"a","properties":{"href":"https://www.toptal.com/web#hiring-guide","rel":["noopener","noreferrer"],"target":"_blank"},"children":[{"type":"text","value":"to check out this article"}]},{"type":"text","value":" from Toptal. "}]}]},{"type":"comment","value":"kg-card-end: html"}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"typography","heading":"Typography"},{"id":"colors","heading":"Colors"},{"id":"images","heading":"Images"},{"id":"icons","heading":"Icons"},{"id":"spacing-layout","heading":"Spacing/Layout"},{"id":"conclusion","heading":"Conclusion","items":[{"id":"error-missing-id","heading":"Suggested Read"}]}]},"featureImageSharp":{"base":"photo-1542744094-3a31f272c490.jpg","publicURL":"/static/6f6b73f2356f89c8be0c82d9fe78a145/photo-1542744094-3a31f272c490.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAIB/9oADAMBAAIQAxAAAAHMohtiQwP/xAAaEAADAAMBAAAAAAAAAAAAAAABAgMAERIT/9oACAEBAAEFApT9MWKY89NJirnfKsSP/8QAFhEBAQEAAAAAAAAAAAAAAAAAAAEh/9oACAEDAQE/AdqP/8QAFREBAQAAAAAAAAAAAAAAAAAAECH/2gAIAQIBAT8Bh//EAB0QAAIBBAMAAAAAAAAAAAAAAAAhEQECAxIxQWH/2gAIAQEABj8Crr02TfkXghEycn//xAAaEAADAQEBAQAAAAAAAAAAAAAAAREhYTFx/9oACAEBAAE/IYFZKaYQiS4elxU100CXH8GwMX4//9oADAMBAAIAAwAAABCTH//EABYRAQEBAAAAAAAAAAAAAAAAAAEAQf/aAAgBAwEBPxAwgpf/xAAXEQEBAQEAAAAAAAAAAAAAAAABABEx/9oACAECAQE/EHHZTb//xAAdEAEBAAICAwEAAAAAAAAAAAABEQAhMVFBYZHR/9oACAEBAAE/EEJq6RnD8w1NMKXtt/M1JlBHSnjDy8WTyuTII/QtDB1CKb3fef/Z","aspectRatio":1.4957264957264957,"src":"/static/6f6b73f2356f89c8be0c82d9fe78a145/ea4ab/photo-1542744094-3a31f272c490.jpg","srcSet":"/static/6f6b73f2356f89c8be0c82d9fe78a145/477ba/photo-1542744094-3a31f272c490.jpg 175w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/06776/photo-1542744094-3a31f272c490.jpg 350w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/ea4ab/photo-1542744094-3a31f272c490.jpg 700w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/3055e/photo-1542744094-3a31f272c490.jpg 1050w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/eff08/photo-1542744094-3a31f272c490.jpg 1400w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/4e5f3/photo-1542744094-3a31f272c490.jpg 2000w","srcWebp":"/static/6f6b73f2356f89c8be0c82d9fe78a145/89afa/photo-1542744094-3a31f272c490.webp","srcSetWebp":"/static/6f6b73f2356f89c8be0c82d9fe78a145/9fca7/photo-1542744094-3a31f272c490.webp 175w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/37a4e/photo-1542744094-3a31f272c490.webp 350w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/89afa/photo-1542744094-3a31f272c490.webp 700w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/78e7a/photo-1542744094-3a31f272c490.webp 1050w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/03d34/photo-1542744094-3a31f272c490.webp 1400w,\n/static/6f6b73f2356f89c8be0c82d9fe78a145/49d6b/photo-1542744094-3a31f272c490.webp 2000w","sizes":"(max-width: 700px) 100vw, 700px"}}}},"allGhostPost":{"edges":[{"node":{"id":"Ghost__Post__631f8eea4e918d05f3537b51","title":"How to Get Elements From the DOM Using JavaScript","slug":"how-to-get-elements-from-the-dom-using-javascript","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/09/js-elm.jpg","excerpt":"This tutorial shows you different ways to get an element from the DOM using JavaScript.","custom_excerpt":"This tutorial shows you different ways to get an element from the DOM using JavaScript.","visibility":"public","created_at_pretty":"12 Sep 2022","published_at_pretty":"19 Sep 2022","updated_at_pretty":"19 Sep 2022","created_at":"2022-09-12T19:56:26.000+00:00","published_at":"2022-09-19T10:58:26.000+00:00","updated_at":"2022-09-19T10:58:26.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":{"base":"photo-1592609931095-54a2168ae893-2.jpeg","publicURL":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/photo-1592609931095-54a2168ae893-2.jpeg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwAEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHh6QZDjq//xAAaEAEAAwADAAAAAAAAAAAAAAABAAIREiFB/9oACAEBAAEFApQ6eO+VXFWf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQIBAT8Bp//EABoQAAICAwAAAAAAAAAAAAAAAAABECExMkH/2gAIAQEABj8CwdNXFOP/xAAcEAADAAMAAwAAAAAAAAAAAAAAAREhQVFhcYH/2gAIAQEAAT8heHx6GaR4cOQ+jrqtTQu6lgaVs//aAAwDAQACAAMAAAAQlM//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8QrH//xAAWEQEBAQAAAAAAAAAAAAAAAAABEBH/2gAIAQIBAT8QdE//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhMUFhweH/2gAIAQEAAT8QwaQHozIjaNdvYnh0/qNZJwLU36JcXxL2lqf/2Q==","aspectRatio":1.5028901734104045,"src":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg","srcSet":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/65d8c/photo-1592609931095-54a2168ae893-2.jpg 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/c5f21/photo-1592609931095-54a2168ae893-2.jpg 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/81a53/photo-1592609931095-54a2168ae893-2.jpg 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/4e5f3/photo-1592609931095-54a2168ae893-2.jpg 2000w","srcWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp","srcSetWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/dc8f3/photo-1592609931095-54a2168ae893-2.webp 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/2db4b/photo-1592609931095-54a2168ae893-2.webp 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/f5845/photo-1592609931095-54a2168ae893-2.webp 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/49d6b/photo-1592609931095-54a2168ae893-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null},{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null}],"plaintext":"While creating a website and working with JavaScript, you'll often need to get access to elements in the DOM for different purposes.\n\nThis tutorial shows you different ways to get an element from the DOM using JavaScript.\n\n\ngetElementById\n\nThe getElementById() method allows you to retrieve an element from the DOM using the element's ID.\n\nIf no element exists in the DOM with the supplied ID, null will be returned instead.\n\nFor example:\n\nconst mainElement = document.getElementById('main');\n\n\ngetElementsByTagName\n\nThe getElementsByTagName() allows you to retrieve an HTMLCollection of elements that have the tag name you supply to the method. An example of a tag name is div.\n\nItems in an HTMLCollection can be accessed similarly to how you would access items in an array.\n\nFor example:\n\nconst divElements = document.getElementsByTagName('div');\nconsole.log(divElements[0]);\n\nYou can use this method on any element and not just the document. That way, you can retrieve all children of that element that have the supplied tag name.\n\nFor example:\n\nconst divElements = document.getElementsByTagName('div');\nconst pElements = divElements[0].getElementsByTagName('p');\nconsole.log(pElements);\n\n\ngetElementsByClassName()\n\nThe getElementsByClassName() method allows you to retrieve a live HTMLCollection of elements that have the class name you provide as a parameter.\n\nA live HTMLCollection means that the items in the collection are updated with any updates that happen to the DOM. So, for example, if an item was part of the collection because it had the class provided as a parameter, but then its class was removed, the item will be removed from the collection.\n\nFor example:\n\nconst mainElements = document.getElementsByClassName('main');\nconsole.log(mainElements);\n\n\ngetElementsByName()\n\nThe getElementsByName() method allows you to retrieve elements by the value of the name attribute. For example, you can use it to retrieve input elements that have the name attribute set to email.\n\nThis method returns a live NodeList, which is generally similar to an HTMLCollection, but the items in the list can be accessed through the methods it provides.\n\nFor example:\n\nconst emailElements = document.getElementsByName('email');\nconsole.log(emailElements.item(0));\n\n\nquerySelector\n\nThe querySelector() method allows you to retrieve the first element that matches the specified selector. The selector can be any CSS selector.\n\nFor example:\n\nconst elm = document.querySelector('.main > p');\nconsole.log(elm);\n\nThis method can be used on any element, and not just the document. So, you can use it to retrieve a child element of a parent element that matches the specified selector.\n\nFor example:\n\nconst table = document.querySelector('.main > table');\nconst thead = table.querySelector('thead');\n\n\nquerySelectorAll\n\nThe querySelectorAll() method allows you to retrieve all elements that match the specified selector. This method returns a NodeList.\n\nFor example:\n\nconst elms = document.querySelectorAll('.main > p');\nconsole.log(elms.item(0));\n\nThis method can be used on any element, and not just the document. So, you can use it to retrieve all child elements of a parent element that match the specified selector.\n\nFor example:\n\nconst table = document.querySelector('.main > table');\nconst rows = table.querySelectorAll('tr');\nfor (const row of rows) {\n    console.log(row);\n}\n\n\nchildren\n\nThe children property allows you to retrieve all immediate child elements of the document or any element. This property's type is a live HTMLCollection.\n\nFor example:\n\nconst rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.children);\n}\n\n\nfirstElementChild\n\nThe firstElementChild property allows you to retrieve the first child element of the document or any element.\n\nIf the element does not have children, the value of firstElementChild is null.\n\nFor example:\n\nconst rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.firstElementChild);\n}\n\n\nlastElementChild\n\nThe lastElementChild property allows you to retrieve the last child element of the document or any element.\n\nIf the element does not have children, the value of lastElementChild is null:\n\nconst rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.lastElementChild);\n}\n\n\nscripts\n\nThe scripts property allows you to retrieve all <script> elements in the document. It returns an HTMLCollection of elements.\n\nFor example:\n\nconsole.log(document.scripts);\n\n\nelementFromPoint\n\nThe elementFromPoint() method allows you to retrieve the top element starting from a specified point. It accepts x and y coordinates to locate the point to look for the element.\n\nFor example:\n\nconst elm = document.elemetFromPoint(100, 20);\nconsole.log(elm);\n\n\nelementsFromPoint\n\nThe elementsFromPoint() method allows you to retrieve an array of Elements starting from a specified point until the end of the viewport.\n\nFor example:\n\nconst elms = document.elementsFromPoint(100, 20);\nconsole.log(elm[0]);\n\n\nclosest\n\nThe closest() method available on elements (not on the document) allows you to retrieve the closest ancestor (which are the parents) of the element that matches the specified selector. If no elements are found, the method returns null.\n\nFor example:\n\nconst closestElm = table.closest('div');\nconsole.log(closestElm);\n\n\nnextElementSibling\n\nThe nextElementSibling property on elements (not on the document) allows you to retrieve the element that follows the current element among its parent's child elements.\n\nIf there are no elements after this element, the value of the property will be null.\n\nFor example:\n\nconsole.log(table.nextElementSibling);\n\n\npreviousElementSibling\n\nThe previousElementSibling property on elements (not on the document) allows you to retrieve the element that proceeds the current element among its parent's child elements.\n\nIf there are no elements before this element, the value of the property will be null.\n\nFor example:\n\nconsole.log(table.previousElementSibling);\n\n\nConclusion\n\nThis tutorial explores a list of methods and properties that you can use to retrieve elements in JavaScript. Each have different purposes and can be used differently based on your use case.","html":"<p>While creating a website and working with JavaScript, you'll often need to get access to elements in the DOM for different purposes.</p><p>This tutorial shows you different ways to get an element from the DOM using JavaScript.</p><h2 id=\"getelementbyid\">getElementById</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById\">getElementById()</a> method allows you to retrieve an element from the DOM using the element's ID.</p><p>If no element exists in the DOM with the supplied ID, <code>null</code> will be returned instead.</p><p>For example:</p><pre><code class=\"language-js\">const mainElement = document.getElementById('main');</code></pre><h2 id=\"getelementsbytagname\">getElementsByTagName</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName\">getElementsByTagName()</a> allows you to retrieve an <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection\">HTMLCollection</a> of elements that have the tag name you supply to the method. An example of a tag name is <code>div</code>.</p><p>Items in an <code>HTMLCollection</code> can be accessed similarly to how you would access items in an array.</p><p>For example:</p><pre><code class=\"language-js\">const divElements = document.getElementsByTagName('div');\nconsole.log(divElements[0]);</code></pre><p>You can use this method on any element and not just the <code>document</code>. That way, you can retrieve all children of that element that have the supplied tag name.</p><p>For example:</p><pre><code class=\"language-js\">const divElements = document.getElementsByTagName('div');\nconst pElements = divElements[0].getElementsByTagName('p');\nconsole.log(pElements);</code></pre><h2 id=\"getelementsbyclassname\">getElementsByClassName()</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName\">getElementsByClassName()</a> method allows you to retrieve a live <code>HTMLCollection</code> of elements that have the class name you provide as a parameter.</p><p>A live <code>HTMLCollection</code> means that the items in the collection are updated with any updates that happen to the DOM. So, for example, if an item was part of the collection because it had the class provided as a parameter, but then its class was removed, the item will be removed from the collection.</p><p>For example:</p><pre><code class=\"language-js\">const mainElements = document.getElementsByClassName('main');\nconsole.log(mainElements);</code></pre><h2 id=\"getelementsbyname\">getElementsByName()</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName\">getElementsByName()</a> method allows you to retrieve elements by the value of the <code>name</code> attribute. For example, you can use it to retrieve <code>input</code> elements that have the <code>name</code> attribute set to <code>email</code>.</p><p>This method returns a live <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/NodeList\">NodeList</a>, which is generally similar to an <code>HTMLCollection</code>, but the items in the list can be accessed through the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/NodeList#methods\">methods</a> it provides.</p><p>For example:</p><pre><code class=\"language-js\">const emailElements = document.getElementsByName('email');\nconsole.log(emailElements.item(0));</code></pre><h2 id=\"queryselector\">querySelector</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector\">querySelector()</a> method allows you to retrieve the first element that matches the specified selector. The selector can be any <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors\">CSS selector</a>.</p><p>For example:</p><pre><code class=\"language-js\">const elm = document.querySelector('.main &gt; p');\nconsole.log(elm);</code></pre><p>This method can be used on any element, and not just the <code>document</code>. So, you can use it to retrieve a child element of a parent element that matches the specified selector.</p><p>For example:</p><pre><code class=\"language-js\">const table = document.querySelector('.main &gt; table');\nconst thead = table.querySelector('thead');</code></pre><h2 id=\"queryselectorall\">querySelectorAll</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll\">querySelectorAll()</a> method allows you to retrieve all elements that match the specified selector. This method returns a <code>NodeList</code>.</p><p>For example:</p><pre><code class=\"language-js\">const elms = document.querySelectorAll('.main &gt; p');\nconsole.log(elms.item(0));</code></pre><p>This method can be used on any element, and not just the <code>document</code>. So, you can use it to retrieve all child elements of a parent element that match the specified selector.</p><p>For example:</p><pre><code class=\"language-js\">const table = document.querySelector('.main &gt; table');\nconst rows = table.querySelectorAll('tr');\nfor (const row of rows) {\n    console.log(row);\n}</code></pre><h2 id=\"children\">children</h2><p>The <code>children</code> property allows you to retrieve all immediate child elements of the <code>document</code> or any element. This property's type is a live <code>HTMLCollection</code>.</p><p>For example:</p><pre><code class=\"language-js\">const rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.children);\n}</code></pre><h2 id=\"firstelementchild\">firstElementChild</h2><p>The <code>firstElementChild</code> property allows you to retrieve the first child element of the <code>document</code> or any element.</p><p>If the element does not have children, the value of <code>firstElementChild</code> is <code>null</code>.</p><p>For example:</p><pre><code class=\"language-js\">const rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.firstElementChild);\n}</code></pre><h2 id=\"lastelementchild\">lastElementChild</h2><p>The <code>lastElementChild</code> property allows you to retrieve the last child element of the <code>document</code> or any element.</p><p>If the element does not have children, the value of <code>lastElementChild</code> is <code>null</code>:</p><pre><code class=\"language-js\">const rows = document.querySelectorAll('table tr');\nfor (const row of rows) {\n    console.log(row.lastElementChild);\n}</code></pre><h2 id=\"scripts\">scripts</h2><p>The <code>scripts</code> property allows you to retrieve all <code>&lt;script&gt;</code> elements in the document. It returns an <code>HTMLCollection</code> of elements.</p><p>For example:</p><pre><code class=\"language-js\">console.log(document.scripts);</code></pre><h2 id=\"elementfrompoint\">elementFromPoint</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint\">elementFromPoint()</a> method allows you to retrieve the top element starting from a specified point. It accepts x and y coordinates to locate the point to look for the element.</p><p>For example:</p><pre><code class=\"language-js\">const elm = document.elemetFromPoint(100, 20);\nconsole.log(elm);</code></pre><h2 id=\"elementsfrompoint\">elementsFromPoint</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint\">elementsFromPoint()</a> method allows you to retrieve an array of Elements starting from a specified point until the end of the viewport.</p><p>For example:</p><pre><code class=\"language-js\">const elms = document.elementsFromPoint(100, 20);\nconsole.log(elm[0]);</code></pre><h2 id=\"closest\">closest</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Element/closest\">closest()</a> method available on elements (not on the <code>document</code>) allows you to retrieve the closest ancestor (which are the parents) of the element that matches the specified selector. If no elements are found, the method returns <code>null</code>.</p><p>For example:</p><pre><code class=\"language-js\">const closestElm = table.closest('div');\nconsole.log(closestElm);</code></pre><h2 id=\"nextelementsibling\">nextElementSibling</h2><p>The <code>nextElementSibling</code> property on elements (not on the <code>document</code>) allows you to retrieve the element that follows the current element among its parent's child elements.</p><p>If there are no elements after this element, the value of the property will be <code>null</code>.</p><p>For example:</p><pre><code class=\"language-js\">console.log(table.nextElementSibling);</code></pre><h2 id=\"previouselementsibling\">previousElementSibling</h2><p>The <code>previousElementSibling</code> property on elements (not on the <code>document</code>) allows you to retrieve the element that proceeds the current element among its parent's child elements.</p><p>If there are no elements before this element, the value of the property will be <code>null</code>.</p><p>For example:</p><pre><code class=\"language-js\">console.log(table.previousElementSibling);</code></pre><h2 id=\"conclusion\">Conclusion</h2><p>This tutorial explores a list of methods and properties that you can use to retrieve elements in JavaScript. Each have different purposes and can be used differently based on your use case.</p>","url":"https://backend.shahednasser.com/how-to-get-elements-from-the-dom-using-javascript/","canonical_url":null,"uuid":"059a970c-e25a-42bc-9c72-002c52914f7b","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"631f8eea4e918d05f3537b51","reading_time":4,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>While creating a website and working with JavaScript, you'll often need to get access to elements in the DOM for different purposes.</p><p>This tutorial shows you different ways to get an element from the DOM using JavaScript.</p><h2 id=\"getelementbyid\">getElementById</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById\">getElementById()</a> method allows you to retrieve an element from the DOM using the element's ID.</p><p>If no element exists in the DOM with the supplied ID, <code class=\"language-text\">null</code> will be returned instead.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mainElement <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'main'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"getelementsbytagname\">getElementsByTagName</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName\">getElementsByTagName()</a> allows you to retrieve an <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection\">HTMLCollection</a> of elements that have the tag name you supply to the method. An example of a tag name is <code class=\"language-text\">div</code>.</p><p>Items in an <code class=\"language-text\">HTMLCollection</code> can be accessed similarly to how you would access items in an array.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> divElements <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementsByTagName</span><span class=\"token punctuation\">(</span><span class=\"token string\">'div'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>divElements<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>You can use this method on any element and not just the <code class=\"language-text\">document</code>. That way, you can retrieve all children of that element that have the supplied tag name.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> divElements <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementsByTagName</span><span class=\"token punctuation\">(</span><span class=\"token string\">'div'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> pElements <span class=\"token operator\">=</span> divElements<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">getElementsByTagName</span><span class=\"token punctuation\">(</span><span class=\"token string\">'p'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>pElements<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"getelementsbyclassname\">getElementsByClassName()</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName\">getElementsByClassName()</a> method allows you to retrieve a live <code class=\"language-text\">HTMLCollection</code> of elements that have the class name you provide as a parameter.</p><p>A live <code class=\"language-text\">HTMLCollection</code> means that the items in the collection are updated with any updates that happen to the DOM. So, for example, if an item was part of the collection because it had the class provided as a parameter, but then its class was removed, the item will be removed from the collection.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mainElements <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementsByClassName</span><span class=\"token punctuation\">(</span><span class=\"token string\">'main'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>mainElements<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"getelementsbyname\">getElementsByName()</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName\">getElementsByName()</a> method allows you to retrieve elements by the value of the <code class=\"language-text\">name</code> attribute. For example, you can use it to retrieve <code class=\"language-text\">input</code> elements that have the <code class=\"language-text\">name</code> attribute set to <code class=\"language-text\">email</code>.</p><p>This method returns a live <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/NodeList\">NodeList</a>, which is generally similar to an <code class=\"language-text\">HTMLCollection</code>, but the items in the list can be accessed through the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/NodeList#methods\">methods</a> it provides.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> emailElements <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementsByName</span><span class=\"token punctuation\">(</span><span class=\"token string\">'email'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>emailElements<span class=\"token punctuation\">.</span><span class=\"token function\">item</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"queryselector\">querySelector</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector\">querySelector()</a> method allows you to retrieve the first element that matches the specified selector. The selector can be any <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors\">CSS selector</a>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> elm <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.main > p'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>elm<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This method can be used on any element, and not just the <code class=\"language-text\">document</code>. So, you can use it to retrieve a child element of a parent element that matches the specified selector.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> table <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.main > table'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> thead <span class=\"token operator\">=</span> table<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'thead'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"queryselectorall\">querySelectorAll</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll\">querySelectorAll()</a> method allows you to retrieve all elements that match the specified selector. This method returns a <code class=\"language-text\">NodeList</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> elms <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelectorAll</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.main > p'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>elms<span class=\"token punctuation\">.</span><span class=\"token function\">item</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This method can be used on any element, and not just the <code class=\"language-text\">document</code>. So, you can use it to retrieve all child elements of a parent element that match the specified selector.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> table <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.main > table'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> rows <span class=\"token operator\">=</span> table<span class=\"token punctuation\">.</span><span class=\"token function\">querySelectorAll</span><span class=\"token punctuation\">(</span><span class=\"token string\">'tr'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> row <span class=\"token keyword\">of</span> rows<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>row<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><h2 id=\"children\">children</h2><p>The <code class=\"language-text\">children</code> property allows you to retrieve all immediate child elements of the <code class=\"language-text\">document</code> or any element. This property's type is a live <code class=\"language-text\">HTMLCollection</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> rows <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelectorAll</span><span class=\"token punctuation\">(</span><span class=\"token string\">'table tr'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> row <span class=\"token keyword\">of</span> rows<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>row<span class=\"token punctuation\">.</span>children<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><h2 id=\"firstelementchild\">firstElementChild</h2><p>The <code class=\"language-text\">firstElementChild</code> property allows you to retrieve the first child element of the <code class=\"language-text\">document</code> or any element.</p><p>If the element does not have children, the value of <code class=\"language-text\">firstElementChild</code> is <code class=\"language-text\">null</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> rows <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelectorAll</span><span class=\"token punctuation\">(</span><span class=\"token string\">'table tr'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> row <span class=\"token keyword\">of</span> rows<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>row<span class=\"token punctuation\">.</span>firstElementChild<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><h2 id=\"lastelementchild\">lastElementChild</h2><p>The <code class=\"language-text\">lastElementChild</code> property allows you to retrieve the last child element of the <code class=\"language-text\">document</code> or any element.</p><p>If the element does not have children, the value of <code class=\"language-text\">lastElementChild</code> is <code class=\"language-text\">null</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> rows <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelectorAll</span><span class=\"token punctuation\">(</span><span class=\"token string\">'table tr'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> row <span class=\"token keyword\">of</span> rows<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>row<span class=\"token punctuation\">.</span>lastElementChild<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><h2 id=\"scripts\">scripts</h2><p>The <code class=\"language-text\">scripts</code> property allows you to retrieve all <code class=\"language-text\">&#x3C;script></code> elements in the document. It returns an <code class=\"language-text\">HTMLCollection</code> of elements.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>scripts<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"elementfrompoint\">elementFromPoint</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint\">elementFromPoint()</a> method allows you to retrieve the top element starting from a specified point. It accepts x and y coordinates to locate the point to look for the element.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> elm <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">elemetFromPoint</span><span class=\"token punctuation\">(</span><span class=\"token number\">100</span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>elm<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"elementsfrompoint\">elementsFromPoint</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint\">elementsFromPoint()</a> method allows you to retrieve an array of Elements starting from a specified point until the end of the viewport.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> elms <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">elementsFromPoint</span><span class=\"token punctuation\">(</span><span class=\"token number\">100</span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>elm<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"closest\">closest</h2><p>The <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Element/closest\">closest()</a> method available on elements (not on the <code class=\"language-text\">document</code>) allows you to retrieve the closest ancestor (which are the parents) of the element that matches the specified selector. If no elements are found, the method returns <code class=\"language-text\">null</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> closestElm <span class=\"token operator\">=</span> table<span class=\"token punctuation\">.</span><span class=\"token function\">closest</span><span class=\"token punctuation\">(</span><span class=\"token string\">'div'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>closestElm<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"nextelementsibling\">nextElementSibling</h2><p>The <code class=\"language-text\">nextElementSibling</code> property on elements (not on the <code class=\"language-text\">document</code>) allows you to retrieve the element that follows the current element among its parent's child elements.</p><p>If there are no elements after this element, the value of the property will be <code class=\"language-text\">null</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>table<span class=\"token punctuation\">.</span>nextElementSibling<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"previouselementsibling\">previousElementSibling</h2><p>The <code class=\"language-text\">previousElementSibling</code> property on elements (not on the <code class=\"language-text\">document</code>) allows you to retrieve the element that proceeds the current element among its parent's child elements.</p><p>If there are no elements before this element, the value of the property will be <code class=\"language-text\">null</code>.</p><p>For example:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>table<span class=\"token punctuation\">.</span>previousElementSibling<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"conclusion\">Conclusion</h2><p>This tutorial explores a list of methods and properties that you can use to retrieve elements in JavaScript. Each have different purposes and can be used differently based on your use case.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While creating a website and working with JavaScript, you'll often need to get access to elements in the DOM for different purposes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tutorial shows you different ways to get an element from the DOM using JavaScript."}]},{"type":"element","tagName":"h2","properties":{"id":"getelementbyid"},"children":[{"type":"text","value":"getElementById"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById"},"children":[{"type":"text","value":"getElementById()"}]},{"type":"text","value":" method allows you to retrieve an element from the DOM using the element's ID."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If no element exists in the DOM with the supplied ID, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":" will be returned instead."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" mainElement "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementById"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'main'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"getelementsbytagname"},"children":[{"type":"text","value":"getElementsByTagName"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName"},"children":[{"type":"text","value":"getElementsByTagName()"}]},{"type":"text","value":" allows you to retrieve an "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection"},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":" of elements that have the tag name you supply to the method. An example of a tag name is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"div"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Items in an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":" can be accessed similarly to how you would access items in an array."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" divElements "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementsByTagName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'div'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"divElements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use this method on any element and not just the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":". That way, you can retrieve all children of that element that have the supplied tag name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" divElements "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementsByTagName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'div'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" pElements "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" divElements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementsByTagName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'p'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pElements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"getelementsbyclassname"},"children":[{"type":"text","value":"getElementsByClassName()"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName"},"children":[{"type":"text","value":"getElementsByClassName()"}]},{"type":"text","value":" method allows you to retrieve a live "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":" of elements that have the class name you provide as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A live "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":" means that the items in the collection are updated with any updates that happen to the DOM. So, for example, if an item was part of the collection because it had the class provided as a parameter, but then its class was removed, the item will be removed from the collection."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" mainElements "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementsByClassName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'main'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mainElements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"getelementsbyname"},"children":[{"type":"text","value":"getElementsByName()"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName"},"children":[{"type":"text","value":"getElementsByName()"}]},{"type":"text","value":" method allows you to retrieve elements by the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute. For example, you can use it to retrieve "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" elements that have the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute set to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"email"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method returns a live "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/NodeList"},"children":[{"type":"text","value":"NodeList"}]},{"type":"text","value":", which is generally similar to an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":", but the items in the list can be accessed through the "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/NodeList#methods"},"children":[{"type":"text","value":"methods"}]},{"type":"text","value":" it provides."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" emailElements "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getElementsByName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'email'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"emailElements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"item"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"queryselector"},"children":[{"type":"text","value":"querySelector"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector"},"children":[{"type":"text","value":"querySelector()"}]},{"type":"text","value":" method allows you to retrieve the first element that matches the specified selector. The selector can be any "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors"},"children":[{"type":"text","value":"CSS selector"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" elm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelector"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'.main > p'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"elm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method can be used on any element, and not just the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":". So, you can use it to retrieve a child element of a parent element that matches the specified selector."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" table "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelector"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'.main > table'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" thead "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" table"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelector"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'thead'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"queryselectorall"},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll"},"children":[{"type":"text","value":"querySelectorAll()"}]},{"type":"text","value":" method allows you to retrieve all elements that match the specified selector. This method returns a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NodeList"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" elms "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'.main > p'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"elms"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"item"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method can be used on any element, and not just the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":". So, you can use it to retrieve all child elements of a parent element that match the specified selector."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" table "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelector"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'.main > table'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" table"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'tr'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" rows"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"children"},"children":[{"type":"text","value":"children"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"children"}]},{"type":"text","value":" property allows you to retrieve all immediate child elements of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":" or any element. This property's type is a live "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'table tr'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" rows"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"children"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"firstelementchild"},"children":[{"type":"text","value":"firstElementChild"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"firstElementChild"}]},{"type":"text","value":" property allows you to retrieve the first child element of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":" or any element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the element does not have children, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"firstElementChild"}]},{"type":"text","value":" is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'table tr'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" rows"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"firstElementChild"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lastelementchild"},"children":[{"type":"text","value":"lastElementChild"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lastElementChild"}]},{"type":"text","value":" property allows you to retrieve the last child element of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":" or any element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the element does not have children, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lastElementChild"}]},{"type":"text","value":" is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"querySelectorAll"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'table tr'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" rows"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lastElementChild"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"scripts"},"children":[{"type":"text","value":"scripts"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"scripts"}]},{"type":"text","value":" property allows you to retrieve all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<script>"}]},{"type":"text","value":" elements in the document. It returns an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HTMLCollection"}]},{"type":"text","value":" of elements."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scripts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"elementfrompoint"},"children":[{"type":"text","value":"elementFromPoint"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint"},"children":[{"type":"text","value":"elementFromPoint()"}]},{"type":"text","value":" method allows you to retrieve the top element starting from a specified point. It accepts x and y coordinates to locate the point to look for the element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" elm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"elemetFromPoint"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"elm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"elementsfrompoint"},"children":[{"type":"text","value":"elementsFromPoint"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint"},"children":[{"type":"text","value":"elementsFromPoint()"}]},{"type":"text","value":" method allows you to retrieve an array of Elements starting from a specified point until the end of the viewport."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" elms "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"elementsFromPoint"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"elm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"closest"},"children":[{"type":"text","value":"closest"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Element/closest"},"children":[{"type":"text","value":"closest()"}]},{"type":"text","value":" method available on elements (not on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":") allows you to retrieve the closest ancestor (which are the parents) of the element that matches the specified selector. If no elements are found, the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" closestElm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" table"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"closest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'div'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\nconsole"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"closestElm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"nextelementsibling"},"children":[{"type":"text","value":"nextElementSibling"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nextElementSibling"}]},{"type":"text","value":" property on elements (not on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":") allows you to retrieve the element that follows the current element among its parent's child elements."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there are no elements after this element, the value of the property will be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"table"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nextElementSibling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"previouselementsibling"},"children":[{"type":"text","value":"previousElementSibling"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"previousElementSibling"}]},{"type":"text","value":" property on elements (not on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document"}]},{"type":"text","value":") allows you to retrieve the element that proceeds the current element among its parent's child elements."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there are no elements before this element, the value of the property will be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"table"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"previousElementSibling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tutorial explores a list of methods and properties that you can use to retrieve elements in JavaScript. Each have different purposes and can be used differently based on your use case."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"getelementbyid","heading":"getElementById"},{"id":"getelementsbytagname","heading":"getElementsByTagName"},{"id":"getelementsbyclassname","heading":"getElementsByClassName()"},{"id":"getelementsbyname","heading":"getElementsByName()"},{"id":"queryselector","heading":"querySelector"},{"id":"queryselectorall","heading":"querySelectorAll"},{"id":"children","heading":"children"},{"id":"firstelementchild","heading":"firstElementChild"},{"id":"lastelementchild","heading":"lastElementChild"},{"id":"scripts","heading":"scripts"},{"id":"elementfrompoint","heading":"elementFromPoint"},{"id":"elementsfrompoint","heading":"elementsFromPoint"},{"id":"closest","heading":"closest"},{"id":"nextelementsibling","heading":"nextElementSibling"},{"id":"previouselementsibling","heading":"previousElementSibling"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"js-elm.jpg","publicURL":"/static/e1e7a4c25e44c189d9cf97060890c67b/js-elm.jpg","imageMeta":{"width":1560,"height":876},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAECAwX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHbjcCGH//EABgQAAIDAAAAAAAAAAAAAAAAAAERAAIg/9oACAEBAAEFAmXZwY//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAVEAEBAAAAAAAAAAAAAAAAAAAgMf/aAAgBAQAGPwKL/8QAGRABAQADAQAAAAAAAAAAAAAAAREAITEg/9oACAEBAAE/ISo4zfphcS9J4//aAAwDAQACAAMAAAAQ48//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAcEAEAAwACAwAAAAAAAAAAAAABABExECFBcZH/2gAIAQEAAT8QZWodVOvkb7ZDNYCtoUC7vhB08wKv3P/Z","aspectRatio":1.7857142857142858,"src":"/static/e1e7a4c25e44c189d9cf97060890c67b/ea4ab/js-elm.jpg","srcSet":"/static/e1e7a4c25e44c189d9cf97060890c67b/477ba/js-elm.jpg 175w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/06776/js-elm.jpg 350w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/ea4ab/js-elm.jpg 700w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/3055e/js-elm.jpg 1050w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/eff08/js-elm.jpg 1400w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/81a53/js-elm.jpg 1560w","srcWebp":"/static/e1e7a4c25e44c189d9cf97060890c67b/89afa/js-elm.webp","srcSetWebp":"/static/e1e7a4c25e44c189d9cf97060890c67b/9fca7/js-elm.webp 175w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/37a4e/js-elm.webp 350w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/89afa/js-elm.webp 700w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/78e7a/js-elm.webp 1050w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/03d34/js-elm.webp 1400w,\n/static/e1e7a4c25e44c189d9cf97060890c67b/f5845/js-elm.webp 1560w","sizes":"(max-width: 700px) 100vw, 700px"}}}}},{"node":{"id":"Ghost__Post__626e658e39840e1ac2875199","title":"Local Storage vs Cookies: What's the Difference?","slug":"localstorage-vs-cookies-whats-the-difference","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/05/Local-Storage-vs-Cookies-What-s-the-Difference.jpg","excerpt":"In this article, you'll learn about some of the main differences between local storage and cookies and which you should choose for your websites.","custom_excerpt":"In this article, you'll learn about some of the main differences between local storage and cookies and which you should choose for your websites.","visibility":"public","created_at_pretty":"1 May 2022","published_at_pretty":"5 May 2022","updated_at_pretty":"5 May 2022","created_at":"2022-05-01T10:48:46.000+00:00","published_at":"2022-05-05T08:57:19.000+00:00","updated_at":"2022-05-05T08:57:19.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":{"base":"photo-1592609931095-54a2168ae893-2.jpeg","publicURL":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/photo-1592609931095-54a2168ae893-2.jpeg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwAEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHh6QZDjq//xAAaEAEAAwADAAAAAAAAAAAAAAABAAIREiFB/9oACAEBAAEFApQ6eO+VXFWf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQIBAT8Bp//EABoQAAICAwAAAAAAAAAAAAAAAAABECExMkH/2gAIAQEABj8CwdNXFOP/xAAcEAADAAMAAwAAAAAAAAAAAAAAAREhQVFhcYH/2gAIAQEAAT8heHx6GaR4cOQ+jrqtTQu6lgaVs//aAAwDAQACAAMAAAAQlM//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8QrH//xAAWEQEBAQAAAAAAAAAAAAAAAAABEBH/2gAIAQIBAT8QdE//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhMUFhweH/2gAIAQEAAT8QwaQHozIjaNdvYnh0/qNZJwLU36JcXxL2lqf/2Q==","aspectRatio":1.5028901734104045,"src":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg","srcSet":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/65d8c/photo-1592609931095-54a2168ae893-2.jpg 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/c5f21/photo-1592609931095-54a2168ae893-2.jpg 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/81a53/photo-1592609931095-54a2168ae893-2.jpg 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/4e5f3/photo-1592609931095-54a2168ae893-2.jpg 2000w","srcWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp","srcSetWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/dc8f3/photo-1592609931095-54a2168ae893-2.webp 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/2db4b/photo-1592609931095-54a2168ae893-2.webp 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/f5845/photo-1592609931095-54a2168ae893-2.webp 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/49d6b/photo-1592609931095-54a2168ae893-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null},{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null}],"plaintext":"Cookies in JavaScript are used to read from and store small data related to the\nuser on the browser.\n\nLocal Storage allows you to also read from and store data related to the user on\nthe browser using JavaScript's Storage API\n[https://developer.mozilla.org/en-US/docs/Web/API/Storage].\n\nIn this article, you'll learn about some of the main differences between local\nstorage and cookies and which you should choose for your websites.\n\nWhat are Cookies?\nCookies are pieces of data that are communicated between the server and the\nbrowser in the header of requests. This allows the server to learn more\ninformation about the user accessing the website but also gives you access in\nthe browser to interact with that data.\n\nAn example of using cookies is storing the user's authentication token when they\nare logged in. The server needs to have access to that cookie to authenticate\nthe user in later requests and retrieve data or process actions based on who the\nuser is.\n\nSimilarly, as a developer, you'll probably need access to that token to use it\nfor future requests to send to your server. \n\nCookies on the client-side (the browser) are natively accessed with \ndocument.cookies. When you store cookies on the browser, you can make them\nspecific to a path in a domain name and you can add an expiry date for them.\n\nWhat is the Local Storage?\nLocal Storage is used to read and write data in the browser's storage. The data\nis persistent and has no expiry date. The data is stored in key-value pairs. The\nserver has no access to the client's local storage.\n\nAn example of using local storage is storing the user's color mode preference\n(light or dark mode). This piece of information is generally not necessary for\nthe server to know, and is used to ensure that the user's preference is\nreflected on the website.\n\nLocal storage is specific to the protocol of a domain. So, the information\nreturned for a website with HTTP protocol is different than the information\nreturned for that same website with HTTPS protocol.\n\nLocal storage is accessed using window.localStorage. It's a read-only property\nwith methods like getItem and setItem to access and modify or add data to the\nlocal storage.\n\nWhat are the Differences Between Cookies and Local Storage\nAccess on the Server\nAs mentioned in the previous sections, the server can access the client's\ncookies but not the data stored in the client's storage.\n\nSo, in use cases where it's important for the server to have access to a set of\ndata, you should use cookies.\n\nData Size\nThere's a big difference between the size of data that cookies can hold and that\nof the local storage. For cookies, the maximum size is 4096 bytes, whereas for\nlocal storage it's 5MB.\n\nFor that reason, cookies should not be used to store large pieces of data. For\nexample, if you want to store the user's details in the browser then it's best\nto store them in the local storage.\n\nData Expiry\nCookies have an expiry date. You can set it to a very far away date, but it\nstill expires. On the other hand, data in the local storage does not expire and\nwill be available any time the user opens your website. The only way to clear\nlocal storage is either manually with code or if the user clears their browser\nstorage manually.\n\nBetter API\nUnless you're using a JavaScript library to facilitate this, it can be a hassle\nto read or write cookies using document.cookies as there is no straight way to\ndo it. Here's an example from W3Schools\n[https://www.w3schools.com/js/js_cookies.asp] of the code you'd need to use to\nget and set a cookie by name:\n\nfunction setCookie(cname, cvalue, exdays) {\n  const d = new Date();\n  d.setTime(d.getTime() + (exdays*24*60*60*1000));\n  let expires = \"expires=\"+ d.toUTCString();\n  document.cookie = cname + \"=\" + cvalue + \";\" + expires + \";path=/\";\n}\n\nfunction getCookie(cname) {\n  let name = cname + \"=\";\n  let decodedCookie = decodeURIComponent(document.cookie);\n  let ca = decodedCookie.split(';');\n  for(let i = 0; i <ca.length; i++) {\n    let c = ca[i];\n    while (c.charAt(0) == ' ') {\n      c = c.substring(1);\n    }\n    if (c.indexOf(name) == 0) {\n      return c.substring(name.length, c.length);\n    }\n  }\n  return \"\";\n}\n\nAs for local storage, it implements the Web Storage API\n[https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API] \nwhich contains easy-to-use methods to read and write data. Here's an example of\nsetting data in the storage and reading them:\n\nlocalStorage.setItem('name', 'Shahed');\n\n//later\nconst name = localStorage.getItem('name');\n\n\nWhich Should You Choose?\nFrom reading the differences and definitions this should be clear. However, to\nsummarize it and make it even clearer:\n\n 1. If you need to store data that is accessible for both the server and the\n    client, use cookies. Otherwise, use local storage.\n 2. If you need to store larger data, use local storage.\n 3. If you need to store data that does not expire, use local storage.\n 4. If you need easy-to-use methods to access and modify the data stored on the\n    client, use local storage.\n\nConclusion\nIf you're interested to learn more about cookies and local storage, check out\nMDN's documentation for cookies\n[https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie] and \nlocalStorage\n[https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage] for a\nmore thorough explanation of each.","html":"<p>Cookies in JavaScript are used to read from and store small data related to the user on the browser.</p><p>Local Storage allows you to also read from and store data related to the user on the browser using JavaScript's <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Storage\">Storage API</a>.</p><p>In this article, you'll learn about some of the main differences between local storage and cookies and which you should choose for your websites.</p><h2 id=\"what-are-cookies\">What are Cookies?</h2><p>Cookies are pieces of data that are communicated between the server and the browser in the header of requests. This allows the server to learn more information about the user accessing the website but also gives you access in the browser to interact with that data.</p><p>An example of using cookies is storing the user's authentication token when they are logged in. The server needs to have access to that cookie to authenticate the user in later requests and retrieve data or process actions based on who the user is.</p><p>Similarly, as a developer, you'll probably need access to that token to use it for future requests to send to your server. </p><p>Cookies on the client-side (the browser) are natively accessed with <code>document.cookies</code>. When you store cookies on the browser, you can make them specific to a path in a domain name and you can add an expiry date for them.</p><h2 id=\"what-is-the-local-storage\">What is the Local Storage?</h2><p>Local Storage is used to read and write data in the browser's storage. The data is persistent and has no expiry date. The data is stored in key-value pairs. The server has no access to the client's local storage.</p><p>An example of using local storage is storing the user's color mode preference (light or dark mode). This piece of information is generally not necessary for the server to know, and is used to ensure that the user's preference is reflected on the website.</p><p>Local storage is specific to the protocol of a domain. So, the information returned for a website with HTTP protocol is different than the information returned for that same website with HTTPS protocol.</p><p>Local storage is accessed using <code>window.localStorage</code>. It's a read-only property with methods like <code>getItem</code> and <code>setItem</code> to access and modify or add data to the local storage.</p><h2 id=\"what-are-the-differences-between-cookies-and-local-storage\">What are the Differences Between Cookies and Local Storage</h2><h3 id=\"access-on-the-server\">Access on the Server</h3><p>As mentioned in the previous sections, the server can access the client's cookies but not the data stored in the client's storage.</p><p>So, in use cases where it's important for the server to have access to a set of data, you should use cookies.</p><h3 id=\"data-size\">Data Size</h3><p>There's a big difference between the size of data that cookies can hold and that of the local storage. For cookies, the maximum size is 4096 bytes, whereas for local storage it's 5MB.</p><p>For that reason, cookies should not be used to store large pieces of data. For example, if you want to store the user's details in the browser then it's best to store them in the local storage.</p><h3 id=\"data-expiry\">Data Expiry</h3><p>Cookies have an expiry date. You can set it to a very far away date, but it still expires. On the other hand, data in the local storage does not expire and will be available any time the user opens your website. The only way to clear local storage is either manually with code or if the user clears their browser storage manually.</p><h3 id=\"better-api\">Better API</h3><p>Unless you're using a JavaScript library to facilitate this, it can be a hassle to read or write cookies using <code>document.cookies</code> as there is no straight way to do it. Here's an example from <a href=\"https://www.w3schools.com/js/js_cookies.asp\">W3Schools</a> of the code you'd need to use to get and set a cookie by name:</p><pre><code class=\"language-js\">function setCookie(cname, cvalue, exdays) {\n  const d = new Date();\n  d.setTime(d.getTime() + (exdays*24*60*60*1000));\n  let expires = \"expires=\"+ d.toUTCString();\n  document.cookie = cname + \"=\" + cvalue + \";\" + expires + \";path=/\";\n}\n\nfunction getCookie(cname) {\n  let name = cname + \"=\";\n  let decodedCookie = decodeURIComponent(document.cookie);\n  let ca = decodedCookie.split(';');\n  for(let i = 0; i &lt;ca.length; i++) {\n    let c = ca[i];\n    while (c.charAt(0) == ' ') {\n      c = c.substring(1);\n    }\n    if (c.indexOf(name) == 0) {\n      return c.substring(name.length, c.length);\n    }\n  }\n  return \"\";\n}</code></pre><p>As for local storage, it implements the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API\">Web Storage API</a> which contains easy-to-use methods to read and write data. Here's an example of setting data in the storage and reading them:</p><pre><code class=\"language-js\">localStorage.setItem('name', 'Shahed');\n\n//later\nconst name = localStorage.getItem('name');\n</code></pre><h2 id=\"which-should-you-choose\">Which Should You Choose?</h2><p>From reading the differences and definitions this should be clear. However, to summarize it and make it even clearer:</p><ol><li>If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage.</li><li>If you need to store larger data, use local storage.</li><li>If you need to store data that does not expire, use local storage.</li><li>If you need easy-to-use methods to access and modify the data stored on the client, use local storage.</li></ol><h2 id=\"conclusion\">Conclusion</h2><p>If you're interested to learn more about cookies and local storage, check out MDN's documentation for <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\">cookies</a> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage\">localStorage</a> for a more thorough explanation of each. </p>","url":"https://backend.shahednasser.com/localstorage-vs-cookies-whats-the-difference/","canonical_url":null,"uuid":"47fde12d-6a2c-497f-8a8c-e66899a86bfd","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"626e658e39840e1ac2875199","reading_time":3,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Cookies in JavaScript are used to read from and store small data related to the user on the browser.</p><p>Local Storage allows you to also read from and store data related to the user on the browser using JavaScript's <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Storage\">Storage API</a>.</p><p>In this article, you'll learn about some of the main differences between local storage and cookies and which you should choose for your websites.</p><h2 id=\"what-are-cookies\">What are Cookies?</h2><p>Cookies are pieces of data that are communicated between the server and the browser in the header of requests. This allows the server to learn more information about the user accessing the website but also gives you access in the browser to interact with that data.</p><p>An example of using cookies is storing the user's authentication token when they are logged in. The server needs to have access to that cookie to authenticate the user in later requests and retrieve data or process actions based on who the user is.</p><p>Similarly, as a developer, you'll probably need access to that token to use it for future requests to send to your server. </p><p>Cookies on the client-side (the browser) are natively accessed with <code class=\"language-text\">document.cookies</code>. When you store cookies on the browser, you can make them specific to a path in a domain name and you can add an expiry date for them.</p><h2 id=\"what-is-the-local-storage\">What is the Local Storage?</h2><p>Local Storage is used to read and write data in the browser's storage. The data is persistent and has no expiry date. The data is stored in key-value pairs. The server has no access to the client's local storage.</p><p>An example of using local storage is storing the user's color mode preference (light or dark mode). This piece of information is generally not necessary for the server to know, and is used to ensure that the user's preference is reflected on the website.</p><p>Local storage is specific to the protocol of a domain. So, the information returned for a website with HTTP protocol is different than the information returned for that same website with HTTPS protocol.</p><p>Local storage is accessed using <code class=\"language-text\">window.localStorage</code>. It's a read-only property with methods like <code class=\"language-text\">getItem</code> and <code class=\"language-text\">setItem</code> to access and modify or add data to the local storage.</p><h2 id=\"what-are-the-differences-between-cookies-and-local-storage\">What are the Differences Between Cookies and Local Storage</h2><h3 id=\"access-on-the-server\">Access on the Server</h3><p>As mentioned in the previous sections, the server can access the client's cookies but not the data stored in the client's storage.</p><p>So, in use cases where it's important for the server to have access to a set of data, you should use cookies.</p><h3 id=\"data-size\">Data Size</h3><p>There's a big difference between the size of data that cookies can hold and that of the local storage. For cookies, the maximum size is 4096 bytes, whereas for local storage it's 5MB.</p><p>For that reason, cookies should not be used to store large pieces of data. For example, if you want to store the user's details in the browser then it's best to store them in the local storage.</p><h3 id=\"data-expiry\">Data Expiry</h3><p>Cookies have an expiry date. You can set it to a very far away date, but it still expires. On the other hand, data in the local storage does not expire and will be available any time the user opens your website. The only way to clear local storage is either manually with code or if the user clears their browser storage manually.</p><h3 id=\"better-api\">Better API</h3><p>Unless you're using a JavaScript library to facilitate this, it can be a hassle to read or write cookies using <code class=\"language-text\">document.cookies</code> as there is no straight way to do it. Here's an example from <a href=\"https://www.w3schools.com/js/js_cookies.asp\">W3Schools</a> of the code you'd need to use to get and set a cookie by name:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">setCookie</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">cname<span class=\"token punctuation\">,</span> cvalue<span class=\"token punctuation\">,</span> exdays</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> d <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  d<span class=\"token punctuation\">.</span><span class=\"token function\">setTime</span><span class=\"token punctuation\">(</span>d<span class=\"token punctuation\">.</span><span class=\"token function\">getTime</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span>exdays<span class=\"token operator\">*</span><span class=\"token number\">24</span><span class=\"token operator\">*</span><span class=\"token number\">60</span><span class=\"token operator\">*</span><span class=\"token number\">60</span><span class=\"token operator\">*</span><span class=\"token number\">1000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">let</span> expires <span class=\"token operator\">=</span> <span class=\"token string\">\"expires=\"</span><span class=\"token operator\">+</span> d<span class=\"token punctuation\">.</span><span class=\"token function\">toUTCString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  document<span class=\"token punctuation\">.</span>cookie <span class=\"token operator\">=</span> cname <span class=\"token operator\">+</span> <span class=\"token string\">\"=\"</span> <span class=\"token operator\">+</span> cvalue <span class=\"token operator\">+</span> <span class=\"token string\">\";\"</span> <span class=\"token operator\">+</span> expires <span class=\"token operator\">+</span> <span class=\"token string\">\";path=/\"</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">getCookie</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">cname</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">let</span> name <span class=\"token operator\">=</span> cname <span class=\"token operator\">+</span> <span class=\"token string\">\"=\"</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">let</span> decodedCookie <span class=\"token operator\">=</span> <span class=\"token function\">decodeURIComponent</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>cookie<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">let</span> ca <span class=\"token operator\">=</span> decodedCookie<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">';'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&#x3C;</span>ca<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> c <span class=\"token operator\">=</span> ca<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">while</span> <span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">.</span><span class=\"token function\">charAt</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token string\">' '</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      c <span class=\"token operator\">=</span> c<span class=\"token punctuation\">.</span><span class=\"token function\">substring</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> c<span class=\"token punctuation\">.</span><span class=\"token function\">substring</span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">,</span> c<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token keyword\">return</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>As for local storage, it implements the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API\">Web Storage API</a> which contains easy-to-use methods to read and write data. Here's an example of setting data in the storage and reading them:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">localStorage<span class=\"token punctuation\">.</span><span class=\"token function\">setItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'name'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Shahed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">//later</span>\n<span class=\"token keyword\">const</span> name <span class=\"token operator\">=</span> localStorage<span class=\"token punctuation\">.</span><span class=\"token function\">getItem</span><span class=\"token punctuation\">(</span><span class=\"token string\">'name'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n</code></pre></div><h2 id=\"which-should-you-choose\">Which Should You Choose?</h2><p>From reading the differences and definitions this should be clear. However, to summarize it and make it even clearer:</p><ol><li>If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage.</li><li>If you need to store larger data, use local storage.</li><li>If you need to store data that does not expire, use local storage.</li><li>If you need easy-to-use methods to access and modify the data stored on the client, use local storage.</li></ol><h2 id=\"conclusion\">Conclusion</h2><p>If you're interested to learn more about cookies and local storage, check out MDN's documentation for <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\">cookies</a> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage\">localStorage</a> for a more thorough explanation of each. </p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Cookies in JavaScript are used to read from and store small data related to the user on the browser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Local Storage allows you to also read from and store data related to the user on the browser using JavaScript's "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Storage"},"children":[{"type":"text","value":"Storage API"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this article, you'll learn about some of the main differences between local storage and cookies and which you should choose for your websites."}]},{"type":"element","tagName":"h2","properties":{"id":"what-are-cookies"},"children":[{"type":"text","value":"What are Cookies?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Cookies are pieces of data that are communicated between the server and the browser in the header of requests. This allows the server to learn more information about the user accessing the website but also gives you access in the browser to interact with that data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of using cookies is storing the user's authentication token when they are logged in. The server needs to have access to that cookie to authenticate the user in later requests and retrieve data or process actions based on who the user is."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, as a developer, you'll probably need access to that token to use it for future requests to send to your server. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Cookies on the client-side (the browser) are natively accessed with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document.cookies"}]},{"type":"text","value":". When you store cookies on the browser, you can make them specific to a path in a domain name and you can add an expiry date for them."}]},{"type":"element","tagName":"h2","properties":{"id":"what-is-the-local-storage"},"children":[{"type":"text","value":"What is the Local Storage?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Local Storage is used to read and write data in the browser's storage. The data is persistent and has no expiry date. The data is stored in key-value pairs. The server has no access to the client's local storage."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of using local storage is storing the user's color mode preference (light or dark mode). This piece of information is generally not necessary for the server to know, and is used to ensure that the user's preference is reflected on the website."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Local storage is specific to the protocol of a domain. So, the information returned for a website with HTTP protocol is different than the information returned for that same website with HTTPS protocol."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Local storage is accessed using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"window.localStorage"}]},{"type":"text","value":". It's a read-only property with methods like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"getItem"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"setItem"}]},{"type":"text","value":" to access and modify or add data to the local storage."}]},{"type":"element","tagName":"h2","properties":{"id":"what-are-the-differences-between-cookies-and-local-storage"},"children":[{"type":"text","value":"What are the Differences Between Cookies and Local Storage"}]},{"type":"element","tagName":"h3","properties":{"id":"access-on-the-server"},"children":[{"type":"text","value":"Access on the Server"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As mentioned in the previous sections, the server can access the client's cookies but not the data stored in the client's storage."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, in use cases where it's important for the server to have access to a set of data, you should use cookies."}]},{"type":"element","tagName":"h3","properties":{"id":"data-size"},"children":[{"type":"text","value":"Data Size"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There's a big difference between the size of data that cookies can hold and that of the local storage. For cookies, the maximum size is 4096 bytes, whereas for local storage it's 5MB."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For that reason, cookies should not be used to store large pieces of data. For example, if you want to store the user's details in the browser then it's best to store them in the local storage."}]},{"type":"element","tagName":"h3","properties":{"id":"data-expiry"},"children":[{"type":"text","value":"Data Expiry"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Cookies have an expiry date. You can set it to a very far away date, but it still expires. On the other hand, data in the local storage does not expire and will be available any time the user opens your website. The only way to clear local storage is either manually with code or if the user clears their browser storage manually."}]},{"type":"element","tagName":"h3","properties":{"id":"better-api"},"children":[{"type":"text","value":"Better API"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unless you're using a JavaScript library to facilitate this, it can be a hassle to read or write cookies using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"document.cookies"}]},{"type":"text","value":" as there is no straight way to do it. Here's an example from "},{"type":"element","tagName":"a","properties":{"href":"https://www.w3schools.com/js/js_cookies.asp"},"children":[{"type":"text","value":"W3Schools"}]},{"type":"text","value":" of the code you'd need to use to get and set a cookie by name:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setCookie"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"cname"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cvalue"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exdays"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" d "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Date"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setTime"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getTime"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"exdays"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" expires "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"expires=\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"toUTCString"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cookie "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" cname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"=\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" cvalue "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" expires "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";path=/\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getCookie"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"cname"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" cname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"=\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" decodedCookie "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"decodeURIComponent"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"document"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cookie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" decodedCookie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"split"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"++"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" c "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"c"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"charAt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"' '"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      c "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" c"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"substring"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"c"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"indexOf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" c"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"substring"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" c"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As for local storage, it implements the "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API"},"children":[{"type":"text","value":"Web Storage API"}]},{"type":"text","value":" which contains easy-to-use methods to read and write data. Here's an example of setting data in the storage and reading them:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"localStorage"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setItem"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Shahed'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//later"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" localStorage"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"getItem"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"which-should-you-choose"},"children":[{"type":"text","value":"Which Should You Choose?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From reading the differences and definitions this should be clear. However, to summarize it and make it even clearer:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If you need to store larger data, use local storage."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If you need to store data that does not expire, use local storage."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If you need easy-to-use methods to access and modify the data stored on the client, use local storage."}]}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you're interested to learn more about cookies and local storage, check out MDN's documentation for "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie"},"children":[{"type":"text","value":"cookies"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage"},"children":[{"type":"text","value":"localStorage"}]},{"type":"text","value":" for a more thorough explanation of each. "}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"what-are-cookies","heading":"What are Cookies?"},{"id":"what-is-the-local-storage","heading":"What is the Local Storage?"},{"id":"what-are-the-differences-between-cookies-and-local-storage","heading":"What are the Differences Between Cookies and Local Storage","items":[{"id":"access-on-the-server","heading":"Access on the Server"},{"id":"data-size","heading":"Data Size"},{"id":"data-expiry","heading":"Data Expiry"},{"id":"better-api","heading":"Better API"}]},{"id":"which-should-you-choose","heading":"Which Should You Choose?"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"Local-Storage-vs-Cookies-What-s-the-Difference.jpg","publicURL":"/static/28f50e0a23d8f16f69da75473f01187c/Local-Storage-vs-Cookies-What-s-the-Difference.jpg","imageMeta":{"width":1560,"height":876},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAAMAAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAdubgD//xAAYEAACAwAAAAAAAAAAAAAAAAAAARESIP/aAAgBAQABBQKzJef/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAXEAADAQAAAAAAAAAAAAAAAAAAATIg/9oACAEBAAY/ApZOf//EABkQAQACAwAAAAAAAAAAAAAAAAEAESAxcf/aAAgBAQABPyFsaSDOx3H/2gAMAwEAAgADAAAAEIPP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGRABAAMBAQAAAAAAAAAAAAAAAREhMQAg/9oACAEBAAE/ECAIOyXxIhOqK8//2Q==","aspectRatio":1.7857142857142858,"src":"/static/28f50e0a23d8f16f69da75473f01187c/ea4ab/Local-Storage-vs-Cookies-What-s-the-Difference.jpg","srcSet":"/static/28f50e0a23d8f16f69da75473f01187c/477ba/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 175w,\n/static/28f50e0a23d8f16f69da75473f01187c/06776/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 350w,\n/static/28f50e0a23d8f16f69da75473f01187c/ea4ab/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 700w,\n/static/28f50e0a23d8f16f69da75473f01187c/3055e/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 1050w,\n/static/28f50e0a23d8f16f69da75473f01187c/eff08/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 1400w,\n/static/28f50e0a23d8f16f69da75473f01187c/81a53/Local-Storage-vs-Cookies-What-s-the-Difference.jpg 1560w","srcWebp":"/static/28f50e0a23d8f16f69da75473f01187c/89afa/Local-Storage-vs-Cookies-What-s-the-Difference.webp","srcSetWebp":"/static/28f50e0a23d8f16f69da75473f01187c/9fca7/Local-Storage-vs-Cookies-What-s-the-Difference.webp 175w,\n/static/28f50e0a23d8f16f69da75473f01187c/37a4e/Local-Storage-vs-Cookies-What-s-the-Difference.webp 350w,\n/static/28f50e0a23d8f16f69da75473f01187c/89afa/Local-Storage-vs-Cookies-What-s-the-Difference.webp 700w,\n/static/28f50e0a23d8f16f69da75473f01187c/78e7a/Local-Storage-vs-Cookies-What-s-the-Difference.webp 1050w,\n/static/28f50e0a23d8f16f69da75473f01187c/03d34/Local-Storage-vs-Cookies-What-s-the-Difference.webp 1400w,\n/static/28f50e0a23d8f16f69da75473f01187c/f5845/Local-Storage-vs-Cookies-What-s-the-Difference.webp 1560w","sizes":"(max-width: 700px) 100vw, 700px"}}}}},{"node":{"id":"Ghost__Post__6248411d39840e1ac2874da6","title":"Optional Chaining in JavaScript","slug":"optional-chaining-in-javascript","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/04/Optional-Chaining-in-JavaScript.jpg","excerpt":"In this article, you’ll learn how and when to use Optional Chaining.","custom_excerpt":"In this article, you’ll learn how and when to use Optional Chaining.","visibility":"public","created_at_pretty":"2 Apr 2022","published_at_pretty":"4 Apr 2022","updated_at_pretty":"4 Apr 2022","created_at":"2022-04-02T12:27:09.000+00:00","published_at":"2022-04-04T09:03:21.000+00:00","updated_at":"2022-04-04T09:03:21.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"murtuzaali","url":"https://backend.shahednasser.com/author/murtuzaali/","name":"Murtuzaali Surti","bio":"Front-end Web Developer, Aspiring Full Stack Developer and a Blogger","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/04/277600836_682640706379781_1485924985509613504_n.jpg","location":null,"website":"https://murtuzaalisurti.github.io","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"murtuzaali","url":"https://backend.shahednasser.com/author/murtuzaali/","name":"Murtuzaali Surti","bio":"Front-end Web Developer, Aspiring Full Stack Developer and a Blogger","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/04/277600836_682640706379781_1485924985509613504_n.jpg","location":null,"website":"https://murtuzaalisurti.github.io","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"277600836_682640706379781_1485924985509613504_n.jpg","publicURL":"/static/ed3da967db374710d628636a2659c3e0/277600836_682640706379781_1485924985509613504_n.jpg","imageMeta":{"width":600,"height":629},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAVABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHh05Wy8oWaUBGD/8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDABIE/9oACAEBAAEFAkTtq+dpjQIFLAKmRuWe3Q3/xAAVEQEBAAAAAAAAAAAAAAAAAAABIP/aAAgBAwEBPwEI/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHRAAAQQCAwAAAAAAAAAAAAAAAQAQESEiMnGRsf/aAAgBAQAGPwKFfrZI0O2mLWgHDf/EABkQAQEAAwEAAAAAAAAAAAAAAAERABAhQf/aAAgBAQABPyFoMsiHLytFfDzEWFAJWpQCGlwGG5r1r//aAAwDAQACAAMAAAAQpAgA/8QAFxEBAQEBAAAAAAAAAAAAAAAAARARIf/aAAgBAwEBPxAEWanJ/8QAFxEBAQEBAAAAAAAAAAAAAAAAARARIf/aAAgBAgEBPxBUcmD2f//EAB8QAQABAwUBAQAAAAAAAAAAAAERABAxIUFRYYGRof/aAAgBAQABPxAtn45cB6pQhAwQFjJgh3+2E5Rvrb9qYQVtMfem0JulmgnW9R/aVCXk10t//9k=","aspectRatio":0.9655172413793104,"src":"/static/ed3da967db374710d628636a2659c3e0/31709/277600836_682640706379781_1485924985509613504_n.jpg","srcSet":"/static/ed3da967db374710d628636a2659c3e0/f340b/277600836_682640706379781_1485924985509613504_n.jpg 28w,\n/static/ed3da967db374710d628636a2659c3e0/22d64/277600836_682640706379781_1485924985509613504_n.jpg 55w,\n/static/ed3da967db374710d628636a2659c3e0/31709/277600836_682640706379781_1485924985509613504_n.jpg 110w,\n/static/ed3da967db374710d628636a2659c3e0/aa249/277600836_682640706379781_1485924985509613504_n.jpg 165w,\n/static/ed3da967db374710d628636a2659c3e0/0dc33/277600836_682640706379781_1485924985509613504_n.jpg 220w,\n/static/ed3da967db374710d628636a2659c3e0/c3638/277600836_682640706379781_1485924985509613504_n.jpg 600w","srcWebp":"/static/ed3da967db374710d628636a2659c3e0/8678c/277600836_682640706379781_1485924985509613504_n.webp","srcSetWebp":"/static/ed3da967db374710d628636a2659c3e0/59cda/277600836_682640706379781_1485924985509613504_n.webp 28w,\n/static/ed3da967db374710d628636a2659c3e0/7da75/277600836_682640706379781_1485924985509613504_n.webp 55w,\n/static/ed3da967db374710d628636a2659c3e0/8678c/277600836_682640706379781_1485924985509613504_n.webp 110w,\n/static/ed3da967db374710d628636a2659c3e0/f282e/277600836_682640706379781_1485924985509613504_n.webp 165w,\n/static/ed3da967db374710d628636a2659c3e0/a7b21/277600836_682640706379781_1485924985509613504_n.webp 220w,\n/static/ed3da967db374710d628636a2659c3e0/fc91b/277600836_682640706379781_1485924985509613504_n.webp 600w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":{"base":"photo-1592609931095-54a2168ae893-2.jpeg","publicURL":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/photo-1592609931095-54a2168ae893-2.jpeg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwAEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHh6QZDjq//xAAaEAEAAwADAAAAAAAAAAAAAAABAAIREiFB/9oACAEBAAEFApQ6eO+VXFWf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQIBAT8Bp//EABoQAAICAwAAAAAAAAAAAAAAAAABECExMkH/2gAIAQEABj8CwdNXFOP/xAAcEAADAAMAAwAAAAAAAAAAAAAAAREhQVFhcYH/2gAIAQEAAT8heHx6GaR4cOQ+jrqtTQu6lgaVs//aAAwDAQACAAMAAAAQlM//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8QrH//xAAWEQEBAQAAAAAAAAAAAAAAAAABEBH/2gAIAQIBAT8QdE//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhMUFhweH/2gAIAQEAAT8QwaQHozIjaNdvYnh0/qNZJwLU36JcXxL2lqf/2Q==","aspectRatio":1.5028901734104045,"src":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg","srcSet":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/65d8c/photo-1592609931095-54a2168ae893-2.jpg 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/c5f21/photo-1592609931095-54a2168ae893-2.jpg 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/d5c54/photo-1592609931095-54a2168ae893-2.jpg 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/81a53/photo-1592609931095-54a2168ae893-2.jpg 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/4e5f3/photo-1592609931095-54a2168ae893-2.jpg 2000w","srcWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp","srcSetWebp":"/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/dc8f3/photo-1592609931095-54a2168ae893-2.webp 260w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/2db4b/photo-1592609931095-54a2168ae893-2.webp 520w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/e4875/photo-1592609931095-54a2168ae893-2.webp 1040w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/f5845/photo-1592609931095-54a2168ae893-2.webp 1560w,\n/static/d0b2a96dbeaa1da6e72d54edc3ccc8e2/49d6b/photo-1592609931095-54a2168ae893-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null},{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null}],"plaintext":"Optional Chaining in JavaScript is used to return undefined for accessing an\nobject property that doesn't exist and whose parent property is nullish (null or\nundefined).\n\nIf you are unaware that property exists or not and you want to avoid the error\nthat's being thrown, you may want to use optional chaining to get around with\nit.\n\nIn this article, you’ll learn how and when to use Optional Chaining. You’ll also\nlearn when not to use Optional Chaining in JavaScript.\n\nHow it Works\nFirst, let's explore what can go wrong when accessing a property in a nested\nobject.\n\nlet person = {\n    name: \"Murtuza\",\n    work: () => {\n        return \"Software Developer\"\n    },\n    socials: {\n        github: {\n            username: \"murtuzaalisurti\",\n            link: \"https://github.com/murtuzaalisurti\",\n            proUser: {\n                is: 'no'\n            }\n        },\n        linkedin: {\n            username: \"murtuzaali-surti\",\n            link: \"https://linkedin.com/in/murtuzaali-surti\"\n        },\n        twitter: {\n            username: \"murtuza_surti\",\n            link: \"https://twitter.com/murtuza_surti\"\n        }\n    }\n}\n\nIn the above object, let's try to access the property link nested within the\nproperty website. Can we?\n\nconsole.log(person.website.link); //an error will be thrown\n\nWe get an error,\n\nCannot read property 'link' of undefined\n\nThe property website doesn't exist in the object!\n\nBut, let's add the property website to the root object and set the value of it\nas null.\n\nwebsite: null\n\nLet's check if this works,\n\nconsole.log(person.website.link); //an error will be thrown\n\nWe get a similar error,\n\nCannot read property 'link' of null\n\nAs mentioned in the above definition, we can use optional chaining to handle\nthese types of errors! Here's how we can do that.\n\nSyntax\n// website: property to validate\n// link: property to access\nwebsite?.link\n\nThe operator ?. will check if the property on its left-hand side is null or\nundefined and if that's the case, then it will simply return undefined without\nthrowing any errors. In other words, this is also known as short-circuiting.\nOtherwise, it will return the value of the property on its right-hand side.\n\nNot just that, you can also invoke a function if it exists using optional\nchaining.\n\nperson.work?.(args)\n\nAlso, you can access properties using [] brackets.\n\nperson.socials.github?.[\"username\"]\n\nWhat You Can’t Do\n * You cannot apply optional chaining to the objects that are not declared yet.\n   For example:\n\nobject?.prop // object is not defined\n\nWe haven't declared object, thus it will throw an error.\n\n * You cannot assign a value to this expression. In other words, the optional\n   chaining expression can't be on the left-hand side.\n\nThe below code is not valid.\n\nperson.socials.github?.[\"username\"] = \"name\" // not valid\n\nWhen to Use Optional Chaining?\nIt's important to note that optional chaining should not be used when it's not\nnecessary to do so. Only use optional chaining when you know that the property\nthat you want to access is optional and not mandatory.\n\nFor example, in our object person, we can keep the social media platforms\noptional, so we are not sure if a user has a social media account or not on a\nparticular platform. For that, we can use optional chaining to check if a user\nhas a social media account on a particular platform and if it exists, get the\nusername.\n\nperson.socials.github?.[\"username\"]\n\nBut, if we place the optional chaining operator at the root object, then it\ndoesn't make any sense because the root object i.e. person must exist and if it\ndoesn't exist, we should get an error!\n\nConclusion\nIn this article, you learned what Optional Chaining in JavaScript is, how it\nworks when to use it, and when not to use it.\n\nTo learn more about how Optional Chaining works, make sure to check out the MDN\ndocumentation\n[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#specifications] \non it for more details.","html":"<p>Optional Chaining in JavaScript is used to return <code>undefined</code> for accessing an object property that doesn't exist and whose parent property is <em><strong>nullish (null or undefined).</strong></em></p><p>If you are unaware that property exists or not and you want to avoid the error that's being thrown, you may want to use optional chaining to get around with it.</p><p>In this article, you’ll learn how and when to use Optional Chaining. You’ll also learn when not to use Optional Chaining in JavaScript.</p><h2 id=\"how-it-works\">How it Works</h2><p>First, let's explore what can go wrong when accessing a property in a nested object.</p><pre><code class=\"language-js\">let person = {\n    name: \"Murtuza\",\n    work: () =&gt; {\n        return \"Software Developer\"\n    },\n    socials: {\n        github: {\n            username: \"murtuzaalisurti\",\n            link: \"https://github.com/murtuzaalisurti\",\n            proUser: {\n                is: 'no'\n            }\n        },\n        linkedin: {\n            username: \"murtuzaali-surti\",\n            link: \"https://linkedin.com/in/murtuzaali-surti\"\n        },\n        twitter: {\n            username: \"murtuza_surti\",\n            link: \"https://twitter.com/murtuza_surti\"\n        }\n    }\n}</code></pre><p>In the above object, let's try to access the property link nested within the property website. Can we?</p><pre><code class=\"language-js\">console.log(person.website.link); //an error will be thrown</code></pre><p>We get an error,</p><pre><code class=\"language-js\">Cannot read property 'link' of undefined</code></pre><p>The property website doesn't exist in the object!</p><p>But, let's add the property <code>website</code> to the root object and set the value of it as <code>null</code>.</p><pre><code class=\"language-js\">website: null</code></pre><p>Let's check if this works,</p><pre><code class=\"language-js\">console.log(person.website.link); //an error will be thrown</code></pre><p>We get a similar error,</p><pre><code class=\"language-js\">Cannot read property 'link' of null</code></pre><p>As mentioned in the above definition, we can use optional chaining to handle these types of errors! Here's how we can do that.</p><h2 id=\"syntax\">Syntax</h2><pre><code class=\"language-js\">// website: property to validate\n// link: property to access\nwebsite?.link</code></pre><p>The operator ?. will check if the property on its left-hand side is null or undefined and if that's the case, then it will simply return undefined without throwing any errors. In other words, this is also known as short-circuiting. Otherwise, it will return the value of the property on its right-hand side.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/04/rUOx6qB.gif\" class=\"kg-image\" alt loading=\"lazy\" width=\"1141\" height=\"139\"></figure><p>Not just that, you can also invoke a function if it exists using optional chaining.</p><pre><code class=\"language-js\">person.work?.(args)</code></pre><p>Also, you can access properties using [] brackets.</p><pre><code class=\"language-js\">person.socials.github?.[\"username\"]</code></pre><h2 id=\"what-you-can%E2%80%99t-do\">What You Can’t Do</h2><ul><li>You cannot apply optional chaining to the objects that are not declared yet. For example:</li></ul><pre><code class=\"language-js\">object?.prop // object is not defined</code></pre><p>We haven't declared <code>object</code>, thus it will throw an error.</p><ul><li>You cannot assign a value to this expression. In other words, the optional chaining expression can't be on the left-hand side.</li></ul><p>The below code is not valid.</p><pre><code class=\"language-js\">person.socials.github?.[\"username\"] = \"name\" // not valid</code></pre><h2 id=\"when-to-use-optional-chaining\">When to Use Optional Chaining?</h2><p>It's important to note that optional chaining should not be used when it's not necessary to do so. Only use optional chaining when you know that the property that you want to access is optional and not mandatory.</p><p>For example, in our object <code>person</code>, we can keep the social media platforms optional, so we are not sure if a user has a social media account or not on a particular platform. For that, we can use optional chaining to check if a user has a social media account on a particular platform and if it exists, get the username.</p><pre><code class=\"language-js\">person.socials.github?.[\"username\"]</code></pre><p>But, if we place the optional chaining operator at the root object, then it doesn't make any sense because the root object i.e. person must exist and if it doesn't exist, we should get an error!</p><h2 id=\"conclusion\">Conclusion</h2><p>In this article, you learned what Optional Chaining in JavaScript is, how it works when to use it, and when not to use it.</p><p>To learn more about how Optional Chaining works, make sure to check out the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#specifications\">MDN documentation</a> on it for more details.</p>","url":"https://backend.shahednasser.com/optional-chaining-in-javascript/","canonical_url":"https://syntackle.live/blog/optional-chaining-in-javascript-D6SuXGtu0-K5hhtZUqqc/","uuid":"8880895a-5b7b-4f7d-9923-ef9d1b51dbb8","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"6248411d39840e1ac2874da6","reading_time":3,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Optional Chaining in JavaScript is used to return <code class=\"language-text\">undefined</code> for accessing an object property that doesn't exist and whose parent property is <em><strong>nullish (null or undefined).</strong></em></p><p>If you are unaware that property exists or not and you want to avoid the error that's being thrown, you may want to use optional chaining to get around with it.</p><p>In this article, you’ll learn how and when to use Optional Chaining. You’ll also learn when not to use Optional Chaining in JavaScript.</p><h2 id=\"how-it-works\">How it Works</h2><p>First, let's explore what can go wrong when accessing a property in a nested object.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> person <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">name</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Murtuza\"</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">work</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">return</span> <span class=\"token string\">\"Software Developer\"</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">socials</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">github</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> <span class=\"token string\">\"murtuzaalisurti\"</span><span class=\"token punctuation\">,</span>\n            <span class=\"token literal-property property\">link</span><span class=\"token operator\">:</span> <span class=\"token string\">\"https://github.com/murtuzaalisurti\"</span><span class=\"token punctuation\">,</span>\n            <span class=\"token literal-property property\">proUser</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token literal-property property\">is</span><span class=\"token operator\">:</span> <span class=\"token string\">'no'</span>\n            <span class=\"token punctuation\">}</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">linkedin</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> <span class=\"token string\">\"murtuzaali-surti\"</span><span class=\"token punctuation\">,</span>\n            <span class=\"token literal-property property\">link</span><span class=\"token operator\">:</span> <span class=\"token string\">\"https://linkedin.com/in/murtuzaali-surti\"</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n        <span class=\"token literal-property property\">twitter</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token literal-property property\">username</span><span class=\"token operator\">:</span> <span class=\"token string\">\"murtuza_surti\"</span><span class=\"token punctuation\">,</span>\n            <span class=\"token literal-property property\">link</span><span class=\"token operator\">:</span> <span class=\"token string\">\"https://twitter.com/murtuza_surti\"</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>In the above object, let's try to access the property link nested within the property website. Can we?</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>person<span class=\"token punctuation\">.</span>website<span class=\"token punctuation\">.</span>link<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//an error will be thrown</span></code></pre></div><p>We get an error,</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">Cannot read property <span class=\"token string\">'link'</span> <span class=\"token keyword\">of</span> <span class=\"token keyword\">undefined</span></code></pre></div><p>The property website doesn't exist in the object!</p><p>But, let's add the property <code class=\"language-text\">website</code> to the root object and set the value of it as <code class=\"language-text\">null</code>.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token literal-property property\">website</span><span class=\"token operator\">:</span> <span class=\"token keyword\">null</span></code></pre></div><p>Let's check if this works,</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>person<span class=\"token punctuation\">.</span>website<span class=\"token punctuation\">.</span>link<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//an error will be thrown</span></code></pre></div><p>We get a similar error,</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">Cannot read property <span class=\"token string\">'link'</span> <span class=\"token keyword\">of</span> <span class=\"token keyword\">null</span></code></pre></div><p>As mentioned in the above definition, we can use optional chaining to handle these types of errors! Here's how we can do that.</p><h2 id=\"syntax\">Syntax</h2><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// website: property to validate</span>\n<span class=\"token comment\">// link: property to access</span>\nwebsite<span class=\"token operator\">?.</span>link</code></pre></div><p>The operator ?. will check if the property on its left-hand side is null or undefined and if that's the case, then it will simply return undefined without throwing any errors. In other words, this is also known as short-circuiting. Otherwise, it will return the value of the property on its right-hand side.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/04/rUOx6qB.gif\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1141\" height=\"139\"></figure><p>Not just that, you can also invoke a function if it exists using optional chaining.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">person<span class=\"token punctuation\">.</span>work<span class=\"token operator\">?.</span><span class=\"token punctuation\">(</span>args<span class=\"token punctuation\">)</span></code></pre></div><p>Also, you can access properties using [] brackets.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">person<span class=\"token punctuation\">.</span>socials<span class=\"token punctuation\">.</span>github<span class=\"token operator\">?.</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"username\"</span><span class=\"token punctuation\">]</span></code></pre></div><h2 id=\"what-you-can%E2%80%99t-do\">What You Can’t Do</h2><ul><li>You cannot apply optional chaining to the objects that are not declared yet. For example:</li></ul><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">object<span class=\"token operator\">?.</span>prop <span class=\"token comment\">// object is not defined</span></code></pre></div><p>We haven't declared <code class=\"language-text\">object</code>, thus it will throw an error.</p><ul><li>You cannot assign a value to this expression. In other words, the optional chaining expression can't be on the left-hand side.</li></ul><p>The below code is not valid.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">person<span class=\"token punctuation\">.</span>socials<span class=\"token punctuation\">.</span>github<span class=\"token operator\">?.</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"username\"</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"name\"</span> <span class=\"token comment\">// not valid</span></code></pre></div><h2 id=\"when-to-use-optional-chaining\">When to Use Optional Chaining?</h2><p>It's important to note that optional chaining should not be used when it's not necessary to do so. Only use optional chaining when you know that the property that you want to access is optional and not mandatory.</p><p>For example, in our object <code class=\"language-text\">person</code>, we can keep the social media platforms optional, so we are not sure if a user has a social media account or not on a particular platform. For that, we can use optional chaining to check if a user has a social media account on a particular platform and if it exists, get the username.</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">person<span class=\"token punctuation\">.</span>socials<span class=\"token punctuation\">.</span>github<span class=\"token operator\">?.</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"username\"</span><span class=\"token punctuation\">]</span></code></pre></div><p>But, if we place the optional chaining operator at the root object, then it doesn't make any sense because the root object i.e. person must exist and if it doesn't exist, we should get an error!</p><h2 id=\"conclusion\">Conclusion</h2><p>In this article, you learned what Optional Chaining in JavaScript is, how it works when to use it, and when not to use it.</p><p>To learn more about how Optional Chaining works, make sure to check out the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#specifications\">MDN documentation</a> on it for more details.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Optional Chaining in JavaScript is used to return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"undefined"}]},{"type":"text","value":" for accessing an object property that doesn't exist and whose parent property is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nullish (null or undefined)."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are unaware that property exists or not and you want to avoid the error that's being thrown, you may want to use optional chaining to get around with it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this article, you’ll learn how and when to use Optional Chaining. You’ll also learn when not to use Optional Chaining in JavaScript."}]},{"type":"element","tagName":"h2","properties":{"id":"how-it-works"},"children":[{"type":"text","value":"How it Works"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, let's explore what can go wrong when accessing a property in a nested object."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Murtuza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function-variable","function"]},"children":[{"type":"text","value":"work"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Software Developer\""}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"socials"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"github"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"username"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"murtuzaalisurti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"link"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://github.com/murtuzaalisurti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"proUser"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"is"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'no'"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"linkedin"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"username"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"murtuzaali-surti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"link"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://linkedin.com/in/murtuzaali-surti\""}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"twitter"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"username"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"murtuza_surti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"link"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://twitter.com/murtuza_surti\""}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above object, let's try to access the property link nested within the property website. Can we?"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"website"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"link"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//an error will be thrown"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We get an error,"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"Cannot read property "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'link'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"undefined"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The property website doesn't exist in the object!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But, let's add the property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"website"}]},{"type":"text","value":" to the root object and set the value of it as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"null"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"website"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"null"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's check if this works,"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"website"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"link"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//an error will be thrown"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We get a similar error,"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"Cannot read property "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'link'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"null"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As mentioned in the above definition, we can use optional chaining to handle these types of errors! Here's how we can do that."}]},{"type":"element","tagName":"h2","properties":{"id":"syntax"},"children":[{"type":"text","value":"Syntax"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// website: property to validate"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// link: property to access"}]},{"type":"text","value":"\nwebsite"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"text","value":"link"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The operator ?. will check if the property on its left-hand side is null or undefined and if that's the case, then it will simply return undefined without throwing any errors. In other words, this is also known as short-circuiting. Otherwise, it will return the value of the property on its right-hand side."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/04/rUOx6qB.gif","className":["kg-image"],"alt":"","loading":"lazy","width":1141,"height":139},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Not just that, you can also invoke a function if it exists using optional chaining."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"work"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"args"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Also, you can access properties using [] brackets."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"socials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"github"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"username\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-you-can%E2%80%99t-do"},"children":[{"type":"text","value":"What You Can’t Do"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You cannot apply optional chaining to the objects that are not declared yet. For example:"}]}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"object"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"text","value":"prop "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// object is not defined"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We haven't declared "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"object"}]},{"type":"text","value":", thus it will throw an error."}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You cannot assign a value to this expression. In other words, the optional chaining expression can't be on the left-hand side."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The below code is not valid."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"socials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"github"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"username\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// not valid"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"when-to-use-optional-chaining"},"children":[{"type":"text","value":"When to Use Optional Chaining?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It's important to note that optional chaining should not be used when it's not necessary to do so. Only use optional chaining when you know that the property that you want to access is optional and not mandatory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, in our object "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person"}]},{"type":"text","value":", we can keep the social media platforms optional, so we are not sure if a user has a social media account or not on a particular platform. For that, we can use optional chaining to check if a user has a social media account on a particular platform and if it exists, get the username."}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"socials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"github"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"username\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But, if we place the optional chaining operator at the root object, then it doesn't make any sense because the root object i.e. person must exist and if it doesn't exist, we should get an error!"}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this article, you learned what Optional Chaining in JavaScript is, how it works when to use it, and when not to use it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To learn more about how Optional Chaining works, make sure to check out the "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#specifications"},"children":[{"type":"text","value":"MDN documentation"}]},{"type":"text","value":" on it for more details."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"how-it-works","heading":"How it Works"},{"id":"syntax","heading":"Syntax"},{"id":"what-you-can%E2%80%99t-do","heading":"What You Can’t Do"},{"id":"when-to-use-optional-chaining","heading":"When to Use Optional Chaining?"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"Optional-Chaining-in-JavaScript.jpg","publicURL":"/static/d9ed2e7558824339cb9d44e26a029146/Optional-Chaining-in-JavaScript.jpg","imageMeta":{"width":1560,"height":876},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAIF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3VAD/8QAFhABAQEAAAAAAAAAAAAAAAAAEQAg/9oACAEBAAEFAmc//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFhABAQEAAAAAAAAAAAAAAAAAMQAg/9oACAEBAAY/AiM//8QAGhAAAgIDAAAAAAAAAAAAAAAAABEBUSAhQf/aAAgBAQABPyFrjXI3xYf/2gAMAwEAAgADAAAAEODP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAHRABAAEDBQAAAAAAAAAAAAAAAREAICFBYaHB0f/aAAgBAQABPxBkQWNZM81h7T2kiVbGz//Z","aspectRatio":1.7857142857142858,"src":"/static/d9ed2e7558824339cb9d44e26a029146/ea4ab/Optional-Chaining-in-JavaScript.jpg","srcSet":"/static/d9ed2e7558824339cb9d44e26a029146/477ba/Optional-Chaining-in-JavaScript.jpg 175w,\n/static/d9ed2e7558824339cb9d44e26a029146/06776/Optional-Chaining-in-JavaScript.jpg 350w,\n/static/d9ed2e7558824339cb9d44e26a029146/ea4ab/Optional-Chaining-in-JavaScript.jpg 700w,\n/static/d9ed2e7558824339cb9d44e26a029146/3055e/Optional-Chaining-in-JavaScript.jpg 1050w,\n/static/d9ed2e7558824339cb9d44e26a029146/eff08/Optional-Chaining-in-JavaScript.jpg 1400w,\n/static/d9ed2e7558824339cb9d44e26a029146/81a53/Optional-Chaining-in-JavaScript.jpg 1560w","srcWebp":"/static/d9ed2e7558824339cb9d44e26a029146/89afa/Optional-Chaining-in-JavaScript.webp","srcSetWebp":"/static/d9ed2e7558824339cb9d44e26a029146/9fca7/Optional-Chaining-in-JavaScript.webp 175w,\n/static/d9ed2e7558824339cb9d44e26a029146/37a4e/Optional-Chaining-in-JavaScript.webp 350w,\n/static/d9ed2e7558824339cb9d44e26a029146/89afa/Optional-Chaining-in-JavaScript.webp 700w,\n/static/d9ed2e7558824339cb9d44e26a029146/78e7a/Optional-Chaining-in-JavaScript.webp 1050w,\n/static/d9ed2e7558824339cb9d44e26a029146/03d34/Optional-Chaining-in-JavaScript.webp 1400w,\n/static/d9ed2e7558824339cb9d44e26a029146/f5845/Optional-Chaining-in-JavaScript.webp 1560w","sizes":"(max-width: 700px) 100vw, 700px"}}}}}]}},"pageContext":{"slug":"linux-tips-for-beginners","prev":"how-i-learned-about-contributing-to-open-source-projects-by-creating-one","next":"web-design-tips-for-web-developers","tag":"beginner","limit":3,"skip":0,"primaryTagCount":62,"collectionPaths":{}}},"staticQueryHashes":["1272700106","1676991999","2138873178","2546165603","2681841279","2938721187","293880488","3052966952","4156497161"]}