{"componentChunkName":"component---src-gatsby-theme-try-ghost-templates-post-js","path":"/website-ideas-to-practice-your-web-development-skills/","result":{"data":{"customPost":{"id":"Ghost__Post__6127ba1b3ed159214d382e6b","title":"Website Ideas To Practice Your Web Development Skills","slug":"website-ideas-to-practice-your-web-development-skills","featured":false,"feature_image":"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1517694712202-14dd9538aa97.jpg","excerpt":"It can be hard to practice your skills as a programmer, especially when you’re a beginner.","custom_excerpt":"It can be hard to practice your skills as a programmer, especially when you’re a beginner.","visibility":"public","created_at_pretty":"26 Oct 2020","published_at_pretty":"10 Jun 2018","updated_at_pretty":"26 Aug 2021","created_at":"2020-10-26T21:23:28.000+00:00","published_at":"2018-06-10T21:22:00.000+00:00","updated_at":"2021-08-26T17:54:54.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":"It can be hard to practice your skills as a programmer, especially when you’re a\nbeginner. There are many websites that offer you exercises, easy or hard, to\npractice your skills and improve yourself.\n\nHowever, the best way to improve your knowledge in programming languages is\nthrough practices that resemble real-life projects. Through these kinds of\nprojects, you can basically get your hands dirty and benefit greatly for future\nreal projects and any problems you might face then.\n\n\n--------------------------------------------------------------------------------\n\nI present to you today a few ideas for projects that can help you test your\nknowledge and expand it. These projects have the elements that can be a part of\nmost websites.\n\nBelow you will find the ideas explained briefly with a few pointers on what\nfeatures you can include. You can include as many features as you want or change\nthe ideas to whatever you desire. I’m just giving you a place to start.\n\nNOTE: These website ideas are for beginners whose skills are basic in\nHTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging\nones. If you are looking to advance your skills, I advise you to follow them in\norder.\n\n\n--------------------------------------------------------------------------------\n\nMovies\nThis is probably one of the most classical project ideas. Simply put, the\nwebsite offers the user a list of movies they can watch.\n\nThe features of the website are basic enough. You can browse all movies\navailable, search through movies, or browse them by category. If you are logged\nin, you can add a comment or rate the movie. Also, you can offer news about the\nmovies or the actors. Any feature you can think of is possible.\n\nWhat you should focus on: The main focus is on the admin’s dashboard. The admin\nis the person that adds, deletes, or edits movies, news, users or comments. If\nthis is your first time developing a project with an admin’s dashboard, then\nthat should be your main focus.\n\n\n--------------------------------------------------------------------------------\n\nOnline bookstore\nThis is somewhat similar to the previous project. Create a website that shows\nthe user a list of available books they can buy.\n\nIf you don’t want to implement the payment part, you don’t have to. You can\nsimulate the buying experience and represent it with a simple email sent to the\nuser indicating what items they have bought.\n\nWhat you should focus on: Many of the features of this website are similar to\nthe ones in the previous project. So, try to implement them differently while\nproviding a seamless user experience. If you haven’t used AJAX before or you’re\nnot very good at it, I advise you to learn how to use it in this website. Also,\ntry to focus on security. Learn more about form validations, securing data\nbefore inserting them into the database, and other ways to make sure your\nwebsite is secure.\n\n\n--------------------------------------------------------------------------------\n\nTests Generator\nThis website can offer many features for users. The main feature is that it\ngives a user the ability to create a multiple choice test and view people’s\nanswers or scores.\n\nYou can add several features to this website. For example, a user that creates a\ntest can make it public or private (accessed by a list of people specified by\nthe user). Another example is that a user can view charts of the questions most\nanswered correctly or least answered correctly. There are so many others as\nwell.\n\nWhat you should focus on: This project has more logic than the previous two.\nFocus in this one on how to implement the main and additional features in the\nmost efficient ways. Also, make sure to implement authorization (which user can\nsee what).\n\n\n--------------------------------------------------------------------------------\n\nTop Ten Lists\nThis is a website that I personally worked on to improve my knowledge of React\n[https://reactjs.org/], but you can create it with the basic and fundamental\nprogramming languages.\n\nSimply, this website lets users create top ten lists about any topic they want.\nAdditional features are users interacting with each other’s lists, voting on\neach other’s lists, messaging each other, among a lot of other features.\n\nWhat you should focus on: A feature you can learn how to implement is giving\nusers the ability to register or login with their social media accounts. You can\ntry Google, Facebook, or Twitter. Another thing you can focus on is learning \nBootstrap [https://getbootstrap.com/] or jQuery [https://jquery.com/] or both.\nBoth of them are very important and can make your projects (and life) easier.\n\n\n--------------------------------------------------------------------------------\n\nBudget Tracker\nA very useful website. The main point of this website is to help users manage\ntheir expenses and incomes.\n\nSome features that can be added to the website are calculating profits,\ninforming the user what are their biggest expenses and how they can save up. You\ncan also generate reports for the user.\n\nWhat you should focus on: on this website, focus on making things simple. This\nwebsite is about helping the user, and the best way you can help them is making\nthe website easy to use. Whether it’s the design or how a user can do a simple\nor big operation, make it as simple as possible.\n\n\n--------------------------------------------------------------------------------\n\nSchool Management\nA very required project by schools and institutes. It’s mostly required as a\nsoftware, but some need it as a website (run locally, mostly) as well.\n\nThis website gives users the ability to store, edit and view students and\nteachers’ information. The school can also manage their finances and their\ncurriculum plans, among other features.\n\nAn institute once asked me to add charts that analyze incomes, expenses, number\nof registered users compared with other users, etc… You can implement that as\nwell.\n\nWhat you should focus on: this website’s core focus is on data. A school will\nfocus mainly on how to view the data and deal with it quickly and efficiently.\nAnother important part is creating backup. Find a way to give the school the\nability to create a backup of their data.\n\nTip: an easy way to generate charts is through Google’s chart web service\n[https://developers.google.com/chart/].\n\n\n--------------------------------------------------------------------------------\n\nConclusion\nThis was a list of a few website ideas you can use to increase your knowledge\nand skills. There are so many more ideas you can think of to create, but this is\nmerely to give you a head start.\n\nYour opinion: what other ideas can beginners use to create websites and improve\ntheir skills?","html":"<p>It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself.</p><p>However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then.</p><hr><p>I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites.</p><p>Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start.</p><p><strong><strong>NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order.</strong></strong></p><hr><h1 id=\"movies\">Movies</h1><p>This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch.</p><p>The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible.</p><p><strong><strong>What you should focus on: </strong></strong>The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus.</p><hr><h1 id=\"online-bookstore\">Online bookstore</h1><p>This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy.</p><p>If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought.</p><p><strong><strong>What you should focus on:</strong></strong> Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure.</p><hr><h1 id=\"tests-generator\">Tests Generator</h1><p>This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores.</p><p>You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well.</p><p><strong><strong>What you should focus on:</strong></strong> This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what).</p><hr><h1 id=\"top-ten-lists\">Top Ten Lists</h1><p>This is a website that I personally worked on to improve my knowledge of <a href=\"https://reactjs.org/\" rel=\"noopener nofollow\">React</a>, but you can create it with the basic and fundamental programming languages.</p><p>Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features.</p><p><strong><strong>What you should focus on:</strong></strong> A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning <a href=\"https://getbootstrap.com/\" rel=\"noopener nofollow\">Bootstrap</a> or <a href=\"https://jquery.com/\" rel=\"noopener nofollow\">jQuery</a> or both. Both of them are very important and can make your projects (and life) easier.</p><hr><h1 id=\"budget-tracker\">Budget Tracker</h1><p>A very useful website. The main point of this website is to help users manage their expenses and incomes.</p><p>Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user.</p><p><strong><strong>What you should focus on: </strong></strong>on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible.</p><hr><h1 id=\"school-management\">School Management</h1><p>A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well.</p><p>This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features.</p><p>An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well.</p><p><strong><strong>What you should focus on: </strong></strong>this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data.</p><p><strong><strong>Tip: </strong></strong>an easy way to generate charts is through <a href=\"https://developers.google.com/chart/\" rel=\"noopener nofollow\">Google’s chart web service</a>.</p><hr><h1 id=\"conclusion\">Conclusion</h1><p>This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start.</p><p><strong><strong>Your opinion: </strong></strong>what other ideas can beginners use to create websites and improve their skills?</p>","url":"https://backend.shahednasser.com/website-ideas-to-practice-your-web-development-skills/","canonical_url":null,"uuid":"9d2a78ac-7148-47c2-a0de-29dcd0cb7da8","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5f973e50b366d4001e86cc33","reading_time":4,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself.</p><p>However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then.</p><hr><p>I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites.</p><p>Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start.</p><p><strong><strong>NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order.</strong></strong></p><hr><h1 id=\"movies\">Movies</h1><p>This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch.</p><p>The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible.</p><p><strong><strong>What you should focus on: </strong></strong>The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus.</p><hr><h1 id=\"online-bookstore\">Online bookstore</h1><p>This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy.</p><p>If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought.</p><p><strong><strong>What you should focus on:</strong></strong> Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure.</p><hr><h1 id=\"tests-generator\">Tests Generator</h1><p>This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores.</p><p>You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well.</p><p><strong><strong>What you should focus on:</strong></strong> This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what).</p><hr><h1 id=\"top-ten-lists\">Top Ten Lists</h1><p>This is a website that I personally worked on to improve my knowledge of <a href=\"https://reactjs.org/\" rel=\"noopener nofollow\">React</a>, but you can create it with the basic and fundamental programming languages.</p><p>Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features.</p><p><strong><strong>What you should focus on:</strong></strong> A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning <a href=\"https://getbootstrap.com/\" rel=\"noopener nofollow\">Bootstrap</a> or <a href=\"https://jquery.com/\" rel=\"noopener nofollow\">jQuery</a> or both. Both of them are very important and can make your projects (and life) easier.</p><hr><h1 id=\"budget-tracker\">Budget Tracker</h1><p>A very useful website. The main point of this website is to help users manage their expenses and incomes.</p><p>Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user.</p><p><strong><strong>What you should focus on: </strong></strong>on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible.</p><hr><h1 id=\"school-management\">School Management</h1><p>A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well.</p><p>This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features.</p><p>An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well.</p><p><strong><strong>What you should focus on: </strong></strong>this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data.</p><p><strong><strong>Tip: </strong></strong>an easy way to generate charts is through <a href=\"https://developers.google.com/chart/\" rel=\"noopener nofollow\">Google’s chart web service</a>.</p><hr><h1 id=\"conclusion\">Conclusion</h1><p>This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start.</p><p><strong><strong>Your opinion: </strong></strong>what other ideas can beginners use to create websites and improve their skills?</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order."}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"movies"},"children":[{"type":"text","value":"Movies"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"online-bookstore"},"children":[{"type":"text","value":"Online bookstore"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"tests-generator"},"children":[{"type":"text","value":"Tests Generator"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what)."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"top-ten-lists"},"children":[{"type":"text","value":"Top Ten Lists"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is a website that I personally worked on to improve my knowledge of "},{"type":"element","tagName":"a","properties":{"href":"https://reactjs.org/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"React"}]},{"type":"text","value":", but you can create it with the basic and fundamental programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning "},{"type":"element","tagName":"a","properties":{"href":"https://getbootstrap.com/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"Bootstrap"}]},{"type":"text","value":" or "},{"type":"element","tagName":"a","properties":{"href":"https://jquery.com/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"jQuery"}]},{"type":"text","value":" or both. Both of them are very important and can make your projects (and life) easier."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"budget-tracker"},"children":[{"type":"text","value":"Budget Tracker"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very useful website. The main point of this website is to help users manage their expenses and incomes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"school-management"},"children":[{"type":"text","value":"School Management"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tip: "}]}]},{"type":"text","value":"an easy way to generate charts is through "},{"type":"element","tagName":"a","properties":{"href":"https://developers.google.com/chart/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"Google’s chart web service"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Your opinion: "}]}]},{"type":"text","value":"what other ideas can beginners use to create websites and improve their skills?"}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"movies","heading":"Movies"},{"id":"online-bookstore","heading":"Online bookstore"},{"id":"tests-generator","heading":"Tests Generator"},{"id":"top-ten-lists","heading":"Top Ten Lists"},{"id":"budget-tracker","heading":"Budget Tracker"},{"id":"school-management","heading":"School Management"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"photo-1517694712202-14dd9538aa97.jpg","publicURL":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/photo-1517694712202-14dd9538aa97.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMEAgX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHCumklKg//xAAYEAEBAAMAAAAAAAAAAAAAAAACAwABEf/aAAgBAQABBQK1aGopR77lZBowI3w5/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFhEAAwAAAAAAAAAAAAAAAAAAARAx/9oACAECAQE/ATF//8QAGhAAAwADAQAAAAAAAAAAAAAAAAERAiExEP/aAAgBAQAGPwLJLLVGu+VrZVTh/8QAGhAAAwEBAQEAAAAAAAAAAAAAAAERITFBUf/aAAgBAQABPyF92MKI84Vfg9jfD6afTCpydGpzZ//aAAwDAQACAAMAAAAQu/8A/8QAFhEAAwAAAAAAAAAAAAAAAAAAARAx/9oACAEDAQE/EBV//8QAFhEAAwAAAAAAAAAAAAAAAAAAEDFR/9oACAECAQE/EIMf/8QAHhABAQABAwUAAAAAAAAAAAAAAREAMUFhIVFxkdH/2gAIAQEAAT8QNQUWoeucdkdNBAPV51wqw1q6gaNy40u1RsfOXQLuz5n/2Q==","aspectRatio":1.5028901734104045,"src":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/d5c54/photo-1517694712202-14dd9538aa97.jpg","srcSet":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/65d8c/photo-1517694712202-14dd9538aa97.jpg 260w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/c5f21/photo-1517694712202-14dd9538aa97.jpg 520w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/d5c54/photo-1517694712202-14dd9538aa97.jpg 1040w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/81a53/photo-1517694712202-14dd9538aa97.jpg 1560w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/4e5f3/photo-1517694712202-14dd9538aa97.jpg 2000w","srcWebp":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/e4875/photo-1517694712202-14dd9538aa97.webp","srcSetWebp":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/dc8f3/photo-1517694712202-14dd9538aa97.webp 260w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/2db4b/photo-1517694712202-14dd9538aa97.webp 520w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/e4875/photo-1517694712202-14dd9538aa97.webp 1040w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/f5845/photo-1517694712202-14dd9538aa97.webp 1560w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/49d6b/photo-1517694712202-14dd9538aa97.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"ghostPost":{"id":"Ghost__Post__6127ba1b3ed159214d382e6b","title":"Website Ideas To Practice Your Web Development Skills","slug":"website-ideas-to-practice-your-web-development-skills","featured":false,"feature_image":"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/photo-1517694712202-14dd9538aa97.jpg","excerpt":"It can be hard to practice your skills as a programmer, especially when you’re a beginner.","custom_excerpt":"It can be hard to practice your skills as a programmer, especially when you’re a beginner.","visibility":"public","created_at_pretty":"26 Oct 2020","published_at_pretty":"10 Jun 2018","updated_at_pretty":"26 Aug 2021","created_at":"2020-10-26T21:23:28.000+00:00","published_at":"2018-06-10T21:22:00.000+00:00","updated_at":"2021-08-26T17:54:54.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":"It can be hard to practice your skills as a programmer, especially when you’re a\nbeginner. There are many websites that offer you exercises, easy or hard, to\npractice your skills and improve yourself.\n\nHowever, the best way to improve your knowledge in programming languages is\nthrough practices that resemble real-life projects. Through these kinds of\nprojects, you can basically get your hands dirty and benefit greatly for future\nreal projects and any problems you might face then.\n\n\n--------------------------------------------------------------------------------\n\nI present to you today a few ideas for projects that can help you test your\nknowledge and expand it. These projects have the elements that can be a part of\nmost websites.\n\nBelow you will find the ideas explained briefly with a few pointers on what\nfeatures you can include. You can include as many features as you want or change\nthe ideas to whatever you desire. I’m just giving you a place to start.\n\nNOTE: These website ideas are for beginners whose skills are basic in\nHTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging\nones. If you are looking to advance your skills, I advise you to follow them in\norder.\n\n\n--------------------------------------------------------------------------------\n\nMovies\nThis is probably one of the most classical project ideas. Simply put, the\nwebsite offers the user a list of movies they can watch.\n\nThe features of the website are basic enough. You can browse all movies\navailable, search through movies, or browse them by category. If you are logged\nin, you can add a comment or rate the movie. Also, you can offer news about the\nmovies or the actors. Any feature you can think of is possible.\n\nWhat you should focus on: The main focus is on the admin’s dashboard. The admin\nis the person that adds, deletes, or edits movies, news, users or comments. If\nthis is your first time developing a project with an admin’s dashboard, then\nthat should be your main focus.\n\n\n--------------------------------------------------------------------------------\n\nOnline bookstore\nThis is somewhat similar to the previous project. Create a website that shows\nthe user a list of available books they can buy.\n\nIf you don’t want to implement the payment part, you don’t have to. You can\nsimulate the buying experience and represent it with a simple email sent to the\nuser indicating what items they have bought.\n\nWhat you should focus on: Many of the features of this website are similar to\nthe ones in the previous project. So, try to implement them differently while\nproviding a seamless user experience. If you haven’t used AJAX before or you’re\nnot very good at it, I advise you to learn how to use it in this website. Also,\ntry to focus on security. Learn more about form validations, securing data\nbefore inserting them into the database, and other ways to make sure your\nwebsite is secure.\n\n\n--------------------------------------------------------------------------------\n\nTests Generator\nThis website can offer many features for users. The main feature is that it\ngives a user the ability to create a multiple choice test and view people’s\nanswers or scores.\n\nYou can add several features to this website. For example, a user that creates a\ntest can make it public or private (accessed by a list of people specified by\nthe user). Another example is that a user can view charts of the questions most\nanswered correctly or least answered correctly. There are so many others as\nwell.\n\nWhat you should focus on: This project has more logic than the previous two.\nFocus in this one on how to implement the main and additional features in the\nmost efficient ways. Also, make sure to implement authorization (which user can\nsee what).\n\n\n--------------------------------------------------------------------------------\n\nTop Ten Lists\nThis is a website that I personally worked on to improve my knowledge of React\n[https://reactjs.org/], but you can create it with the basic and fundamental\nprogramming languages.\n\nSimply, this website lets users create top ten lists about any topic they want.\nAdditional features are users interacting with each other’s lists, voting on\neach other’s lists, messaging each other, among a lot of other features.\n\nWhat you should focus on: A feature you can learn how to implement is giving\nusers the ability to register or login with their social media accounts. You can\ntry Google, Facebook, or Twitter. Another thing you can focus on is learning \nBootstrap [https://getbootstrap.com/] or jQuery [https://jquery.com/] or both.\nBoth of them are very important and can make your projects (and life) easier.\n\n\n--------------------------------------------------------------------------------\n\nBudget Tracker\nA very useful website. The main point of this website is to help users manage\ntheir expenses and incomes.\n\nSome features that can be added to the website are calculating profits,\ninforming the user what are their biggest expenses and how they can save up. You\ncan also generate reports for the user.\n\nWhat you should focus on: on this website, focus on making things simple. This\nwebsite is about helping the user, and the best way you can help them is making\nthe website easy to use. Whether it’s the design or how a user can do a simple\nor big operation, make it as simple as possible.\n\n\n--------------------------------------------------------------------------------\n\nSchool Management\nA very required project by schools and institutes. It’s mostly required as a\nsoftware, but some need it as a website (run locally, mostly) as well.\n\nThis website gives users the ability to store, edit and view students and\nteachers’ information. The school can also manage their finances and their\ncurriculum plans, among other features.\n\nAn institute once asked me to add charts that analyze incomes, expenses, number\nof registered users compared with other users, etc… You can implement that as\nwell.\n\nWhat you should focus on: this website’s core focus is on data. A school will\nfocus mainly on how to view the data and deal with it quickly and efficiently.\nAnother important part is creating backup. Find a way to give the school the\nability to create a backup of their data.\n\nTip: an easy way to generate charts is through Google’s chart web service\n[https://developers.google.com/chart/].\n\n\n--------------------------------------------------------------------------------\n\nConclusion\nThis was a list of a few website ideas you can use to increase your knowledge\nand skills. There are so many more ideas you can think of to create, but this is\nmerely to give you a head start.\n\nYour opinion: what other ideas can beginners use to create websites and improve\ntheir skills?","html":"<p>It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself.</p><p>However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then.</p><hr><p>I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites.</p><p>Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start.</p><p><strong><strong>NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order.</strong></strong></p><hr><h1 id=\"movies\">Movies</h1><p>This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch.</p><p>The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible.</p><p><strong><strong>What you should focus on: </strong></strong>The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus.</p><hr><h1 id=\"online-bookstore\">Online bookstore</h1><p>This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy.</p><p>If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought.</p><p><strong><strong>What you should focus on:</strong></strong> Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure.</p><hr><h1 id=\"tests-generator\">Tests Generator</h1><p>This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores.</p><p>You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well.</p><p><strong><strong>What you should focus on:</strong></strong> This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what).</p><hr><h1 id=\"top-ten-lists\">Top Ten Lists</h1><p>This is a website that I personally worked on to improve my knowledge of <a href=\"https://reactjs.org/\" rel=\"noopener nofollow\">React</a>, but you can create it with the basic and fundamental programming languages.</p><p>Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features.</p><p><strong><strong>What you should focus on:</strong></strong> A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning <a href=\"https://getbootstrap.com/\" rel=\"noopener nofollow\">Bootstrap</a> or <a href=\"https://jquery.com/\" rel=\"noopener nofollow\">jQuery</a> or both. Both of them are very important and can make your projects (and life) easier.</p><hr><h1 id=\"budget-tracker\">Budget Tracker</h1><p>A very useful website. The main point of this website is to help users manage their expenses and incomes.</p><p>Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user.</p><p><strong><strong>What you should focus on: </strong></strong>on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible.</p><hr><h1 id=\"school-management\">School Management</h1><p>A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well.</p><p>This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features.</p><p>An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well.</p><p><strong><strong>What you should focus on: </strong></strong>this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data.</p><p><strong><strong>Tip: </strong></strong>an easy way to generate charts is through <a href=\"https://developers.google.com/chart/\" rel=\"noopener nofollow\">Google’s chart web service</a>.</p><hr><h1 id=\"conclusion\">Conclusion</h1><p>This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start.</p><p><strong><strong>Your opinion: </strong></strong>what other ideas can beginners use to create websites and improve their skills?</p>","url":"https://backend.shahednasser.com/website-ideas-to-practice-your-web-development-skills/","canonical_url":null,"uuid":"9d2a78ac-7148-47c2-a0de-29dcd0cb7da8","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5f973e50b366d4001e86cc33","reading_time":4,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself.</p><p>However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then.</p><hr><p>I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites.</p><p>Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start.</p><p><strong><strong>NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order.</strong></strong></p><hr><h1 id=\"movies\">Movies</h1><p>This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch.</p><p>The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible.</p><p><strong><strong>What you should focus on: </strong></strong>The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus.</p><hr><h1 id=\"online-bookstore\">Online bookstore</h1><p>This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy.</p><p>If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought.</p><p><strong><strong>What you should focus on:</strong></strong> Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure.</p><hr><h1 id=\"tests-generator\">Tests Generator</h1><p>This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores.</p><p>You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well.</p><p><strong><strong>What you should focus on:</strong></strong> This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what).</p><hr><h1 id=\"top-ten-lists\">Top Ten Lists</h1><p>This is a website that I personally worked on to improve my knowledge of <a href=\"https://reactjs.org/\" rel=\"noopener nofollow\">React</a>, but you can create it with the basic and fundamental programming languages.</p><p>Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features.</p><p><strong><strong>What you should focus on:</strong></strong> A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning <a href=\"https://getbootstrap.com/\" rel=\"noopener nofollow\">Bootstrap</a> or <a href=\"https://jquery.com/\" rel=\"noopener nofollow\">jQuery</a> or both. Both of them are very important and can make your projects (and life) easier.</p><hr><h1 id=\"budget-tracker\">Budget Tracker</h1><p>A very useful website. The main point of this website is to help users manage their expenses and incomes.</p><p>Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user.</p><p><strong><strong>What you should focus on: </strong></strong>on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible.</p><hr><h1 id=\"school-management\">School Management</h1><p>A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well.</p><p>This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features.</p><p>An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well.</p><p><strong><strong>What you should focus on: </strong></strong>this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data.</p><p><strong><strong>Tip: </strong></strong>an easy way to generate charts is through <a href=\"https://developers.google.com/chart/\" rel=\"noopener nofollow\">Google’s chart web service</a>.</p><hr><h1 id=\"conclusion\">Conclusion</h1><p>This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start.</p><p><strong><strong>Your opinion: </strong></strong>what other ideas can beginners use to create websites and improve their skills?</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It can be hard to practice your skills as a programmer, especially when you’re a beginner. There are many websites that offer you exercises, easy or hard, to practice your skills and improve yourself."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, the best way to improve your knowledge in programming languages is through practices that resemble real-life projects. Through these kinds of projects, you can basically get your hands dirty and benefit greatly for future real projects and any problems you might face then."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I present to you today a few ideas for projects that can help you test your knowledge and expand it. These projects have the elements that can be a part of most websites."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below you will find the ideas explained briefly with a few pointers on what features you can include. You can include as many features as you want or change the ideas to whatever you desire. I’m just giving you a place to start."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTE: These website ideas are for beginners whose skills are basic in HTML/CSS/Javascript/PHP. They are ordered from easiest to the more challenging ones. If you are looking to advance your skills, I advise you to follow them in order."}]}]}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"movies"},"children":[{"type":"text","value":"Movies"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is probably one of the most classical project ideas. Simply put, the website offers the user a list of movies they can watch."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The features of the website are basic enough. You can browse all movies available, search through movies, or browse them by category. If you are logged in, you can add a comment or rate the movie. Also, you can offer news about the movies or the actors. Any feature you can think of is possible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"The main focus is on the admin’s dashboard. The admin is the person that adds, deletes, or edits movies, news, users or comments. If this is your first time developing a project with an admin’s dashboard, then that should be your main focus."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"online-bookstore"},"children":[{"type":"text","value":"Online bookstore"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is somewhat similar to the previous project. Create a website that shows the user a list of available books they can buy."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you don’t want to implement the payment part, you don’t have to. You can simulate the buying experience and represent it with a simple email sent to the user indicating what items they have bought."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" Many of the features of this website are similar to the ones in the previous project. So, try to implement them differently while providing a seamless user experience. If you haven’t used AJAX before or you’re not very good at it, I advise you to learn how to use it in this website. Also, try to focus on security. Learn more about form validations, securing data before inserting them into the database, and other ways to make sure your website is secure."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"tests-generator"},"children":[{"type":"text","value":"Tests Generator"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This website can offer many features for users. The main feature is that it gives a user the ability to create a multiple choice test and view people’s answers or scores."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can add several features to this website. For example, a user that creates a test can make it public or private (accessed by a list of people specified by the user). Another example is that a user can view charts of the questions most answered correctly or least answered correctly. There are so many others as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" This project has more logic than the previous two. Focus in this one on how to implement the main and additional features in the most efficient ways. Also, make sure to implement authorization (which user can see what)."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"top-ten-lists"},"children":[{"type":"text","value":"Top Ten Lists"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is a website that I personally worked on to improve my knowledge of "},{"type":"element","tagName":"a","properties":{"href":"https://reactjs.org/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"React"}]},{"type":"text","value":", but you can create it with the basic and fundamental programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Simply, this website lets users create top ten lists about any topic they want. Additional features are users interacting with each other’s lists, voting on each other’s lists, messaging each other, among a lot of other features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on:"}]}]},{"type":"text","value":" A feature you can learn how to implement is giving users the ability to register or login with their social media accounts. You can try Google, Facebook, or Twitter. Another thing you can focus on is learning "},{"type":"element","tagName":"a","properties":{"href":"https://getbootstrap.com/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"Bootstrap"}]},{"type":"text","value":" or "},{"type":"element","tagName":"a","properties":{"href":"https://jquery.com/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"jQuery"}]},{"type":"text","value":" or both. Both of them are very important and can make your projects (and life) easier."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"budget-tracker"},"children":[{"type":"text","value":"Budget Tracker"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very useful website. The main point of this website is to help users manage their expenses and incomes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some features that can be added to the website are calculating profits, informing the user what are their biggest expenses and how they can save up. You can also generate reports for the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"on this website, focus on making things simple. This website is about helping the user, and the best way you can help them is making the website easy to use. Whether it’s the design or how a user can do a simple or big operation, make it as simple as possible."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"school-management"},"children":[{"type":"text","value":"School Management"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very required project by schools and institutes. It’s mostly required as a software, but some need it as a website (run locally, mostly) as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This website gives users the ability to store, edit and view students and teachers’ information. The school can also manage their finances and their curriculum plans, among other features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An institute once asked me to add charts that analyze incomes, expenses, number of registered users compared with other users, etc… You can implement that as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What you should focus on: "}]}]},{"type":"text","value":"this website’s core focus is on data. A school will focus mainly on how to view the data and deal with it quickly and efficiently. Another important part is creating backup. Find a way to give the school the ability to create a backup of their data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tip: "}]}]},{"type":"text","value":"an easy way to generate charts is through "},{"type":"element","tagName":"a","properties":{"href":"https://developers.google.com/chart/","rel":["noopener","nofollow"]},"children":[{"type":"text","value":"Google’s chart web service"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"hr","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This was a list of a few website ideas you can use to increase your knowledge and skills. There are so many more ideas you can think of to create, but this is merely to give you a head start."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Your opinion: "}]}]},{"type":"text","value":"what other ideas can beginners use to create websites and improve their skills?"}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"movies","heading":"Movies"},{"id":"online-bookstore","heading":"Online bookstore"},{"id":"tests-generator","heading":"Tests Generator"},{"id":"top-ten-lists","heading":"Top Ten Lists"},{"id":"budget-tracker","heading":"Budget Tracker"},{"id":"school-management","heading":"School Management"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"photo-1517694712202-14dd9538aa97.jpg","publicURL":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/photo-1517694712202-14dd9538aa97.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMEAgX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHCumklKg//xAAYEAEBAAMAAAAAAAAAAAAAAAACAwABEf/aAAgBAQABBQK1aGopR77lZBowI3w5/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFhEAAwAAAAAAAAAAAAAAAAAAARAx/9oACAECAQE/ATF//8QAGhAAAwADAQAAAAAAAAAAAAAAAAERAiExEP/aAAgBAQAGPwLJLLVGu+VrZVTh/8QAGhAAAwEBAQEAAAAAAAAAAAAAAAERITFBUf/aAAgBAQABPyF92MKI84Vfg9jfD6afTCpydGpzZ//aAAwDAQACAAMAAAAQu/8A/8QAFhEAAwAAAAAAAAAAAAAAAAAAARAx/9oACAEDAQE/EBV//8QAFhEAAwAAAAAAAAAAAAAAAAAAEDFR/9oACAECAQE/EIMf/8QAHhABAQABAwUAAAAAAAAAAAAAAREAMUFhIVFxkdH/2gAIAQEAAT8QNQUWoeucdkdNBAPV51wqw1q6gaNy40u1RsfOXQLuz5n/2Q==","aspectRatio":1.5028901734104045,"src":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/d5c54/photo-1517694712202-14dd9538aa97.jpg","srcSet":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/65d8c/photo-1517694712202-14dd9538aa97.jpg 260w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/c5f21/photo-1517694712202-14dd9538aa97.jpg 520w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/d5c54/photo-1517694712202-14dd9538aa97.jpg 1040w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/81a53/photo-1517694712202-14dd9538aa97.jpg 1560w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/4e5f3/photo-1517694712202-14dd9538aa97.jpg 2000w","srcWebp":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/e4875/photo-1517694712202-14dd9538aa97.webp","srcSetWebp":"/static/6c6fc55bcf24f1e3efdaa0001ab5758e/dc8f3/photo-1517694712202-14dd9538aa97.webp 260w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/2db4b/photo-1517694712202-14dd9538aa97.webp 520w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/e4875/photo-1517694712202-14dd9538aa97.webp 1040w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/f5845/photo-1517694712202-14dd9538aa97.webp 1560w,\n/static/6c6fc55bcf24f1e3efdaa0001ab5758e/49d6b/photo-1517694712202-14dd9538aa97.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"prev":{"id":"Ghost__Post__6127ba1b3ed159214d382e71","title":"Chrome Extension Tutorial — Replace Images in Any Website with Pikachu","slug":"chrome-extension-tutorial-replace-images-in-any-website-with-pikachu","featured":false,"feature_image":"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension.gif","excerpt":"What’s a better way to learn how to make a Chrome extension than making the web cuter with Pikachu images?","custom_excerpt":"What’s a better way to learn how to make a Chrome extension than making the web cuter with Pikachu images?","visibility":"public","created_at_pretty":"6 Nov 2020","published_at_pretty":"11 Apr 2019","updated_at_pretty":"26 Aug 2021","created_at":"2020-11-06T18:32:10.000+00:00","published_at":"2019-04-11T18:30:00.000+00:00","updated_at":"2021-08-26T17:55:09.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":"browser-extensions","url":"https://backend.shahednasser.com/tag/browser-extensions/","name":"Browser Extensions","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1616499370260-485b3e5ed653-2.jpeg","description":"Learn more about Browser Extensions through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1616499370260-485b3e5ed653-2.jpeg","publicURL":"/static/17d3062927434b0d9d22f3e45ece68b8/photo-1616499370260-485b3e5ed653-2.jpeg","imageMeta":{"width":2000,"height":1334},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMCBAX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAv/aAAwDAQACEAMQAAABQ+hsRUBAf//EABoQAQADAAMAAAAAAAAAAAAAAAIAAQMEEyL/2gAIAQEAAQUCKpA3nefSo/JALzvlaXP/xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPwGH/8QAFREBAQAAAAAAAAAAAAAAAAAAERD/2gAIAQIBAT8BSf/EAB4QAAEDBAMAAAAAAAAAAAAAAAEAAiEDEBEiMVFh/9oACAEBAAY/Ai1x9CG2/VmEcoVCJwogL//EABoQAQEBAQADAAAAAAAAAAAAAAERACFBcYH/2gAIAQEAAT8h6pD2YW6ifbqrwMDk3W6VFUxyFZ4ib//aAAwDAQACAAMAAAAQLB//xAAXEQEAAwAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/EJs1/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8QFf/EABwQAQEAAwEAAwAAAAAAAAAAAAERACExQVFhgf/aAAgBAQABPxCsM1ex6ZRBIol9InPbigkXRbrDW2E9042KoTWHL8fWeOKEM/Xuf//Z","aspectRatio":1.5028901734104045,"src":"/static/17d3062927434b0d9d22f3e45ece68b8/d5c54/photo-1616499370260-485b3e5ed653-2.jpg","srcSet":"/static/17d3062927434b0d9d22f3e45ece68b8/65d8c/photo-1616499370260-485b3e5ed653-2.jpg 260w,\n/static/17d3062927434b0d9d22f3e45ece68b8/c5f21/photo-1616499370260-485b3e5ed653-2.jpg 520w,\n/static/17d3062927434b0d9d22f3e45ece68b8/d5c54/photo-1616499370260-485b3e5ed653-2.jpg 1040w,\n/static/17d3062927434b0d9d22f3e45ece68b8/81a53/photo-1616499370260-485b3e5ed653-2.jpg 1560w,\n/static/17d3062927434b0d9d22f3e45ece68b8/4e5f3/photo-1616499370260-485b3e5ed653-2.jpg 2000w","srcWebp":"/static/17d3062927434b0d9d22f3e45ece68b8/e4875/photo-1616499370260-485b3e5ed653-2.webp","srcSetWebp":"/static/17d3062927434b0d9d22f3e45ece68b8/dc8f3/photo-1616499370260-485b3e5ed653-2.webp 260w,\n/static/17d3062927434b0d9d22f3e45ece68b8/2db4b/photo-1616499370260-485b3e5ed653-2.webp 520w,\n/static/17d3062927434b0d9d22f3e45ece68b8/e4875/photo-1616499370260-485b3e5ed653-2.webp 1040w,\n/static/17d3062927434b0d9d22f3e45ece68b8/f5845/photo-1616499370260-485b3e5ed653-2.webp 1560w,\n/static/17d3062927434b0d9d22f3e45ece68b8/49d6b/photo-1616499370260-485b3e5ed653-2.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"browser-extensions","url":"https://backend.shahednasser.com/tag/browser-extensions/","name":"Browser Extensions","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1616499370260-485b3e5ed653-2.jpeg","description":"Learn more about Browser Extensions through tutorials, articles, and tips.","meta_title":null,"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},{"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}],"plaintext":"This article was also published on Level Up Coding's gitconnected\n[https://levelup.gitconnected.com/chrome-extension-tutorial-replace-images-in-any-website-with-pikachu-de2a6e3548bb]\n\nWhat’s a better way to learn how to make a Chrome extension than making the web\ncuter with Pikachu images?\n\nIn this tutorial, I will show you how you can create a simple Chrome extension\nthat replaces images on websites with Pikachu images. We will discuss topics\nlike content scripts, background scripts, and messages between scripts in Chrome\nextensions.\n\nThe code for this tutorial is available here\n[https://github.com/shahednasser/pikachu-everywhere].\n\nFirst thing you need to do when creating any chrome extension is start with\ncreating a manifest.json file in the root of the extension’s directory. For now,\nthis file will hold only the following basic information about your extension:\n\nHere is what everything in this file means:\n\n 1. name: The name of the extension. I named the extension “Pikachu Everywhere”\n 2. version: This is the version of the extension. When you publish the\n    extension in the Chrome Web Store and you need to update it, you will have\n    to increase the version number so it’s good to start with a low number.\n 3. description: (Optional) Description of your extension\n 4. manifest_version: As of Chrome 18, developers are required to use manifest\n    version 2, since manifest version 1 is now deprecated.\n 5. icons: (Optional) Here we specify the extension’s icon. This icon will show\n    up in your browser’s toolbar, the Chrome Web Store, and any other place that\n    requires to show it. For that reason, it’s good to include different sizes\n    of the icon. I have used an icon from Free Icons PNG\n    [https://www.freeiconspng.com/img/17360] and resized the icons using \n    https://resizeimage.net/ (You can find the images in the GitHub repository\n    [https://github.com/shahednasser/pikachu-everywhere]). The paths specified\n    for these images is relative to the root of the extension.\n\nWith this, we have the core of any Chrome extension. Next, we’ll add the\nextension to the browser.\n\nGo to chrome://extensions. There, you can toggle developer mode at the top right\ncorner. Once you do that, a new toolbar will show up with three buttons: “Load\nunpacked,” “Pack extension,” and “Update.” Click on “Load unpacked” and choose\nyour extension’s directory.\n\nOnce you do that, you will see that your new chrome extension is now working.\nYes, it does nothing, but it’s working.\n\nIn Chrome extensions, there are two types of scripts you can use. There are\nBackground scripts and Content scripts. Background scripts run (obviously) in\nthe background. They are loaded when needed and unloaded when idle. They are\nused to listen to certain events in your Chrome extension.\n\nContent scripts are scripts loaded once you open a website and injected it into\nit. Once you open a website, they will start running just like any other script\non that website. After you close the website, the content scripts on that\nwebsite will stop running as well.\n\nFor our extension, we want to replace every image on every website with Pikachu\nimages. In order to do that we need to specify a content script that works every\ntime you open a web page.\n\nLet’s create our content script in assets/js/contentScript.js and include the\nfollowing script to test whether our extension is working or not:\n\nOne other thing we need to do is tell Chrome which script do we need to run as a\ncontent script. Add the following lines to manifest.json\n\nHere we are using content_scripts as key with the value as an array. Here’s what\neverything in the array means:\n\n 1. matches: here you can specify which URLs the content script should run on.\n    In our case we want our content script to run on all pages. So, we use the\n    placeholder “<all_urls>” to specify that.\n 2. all_frames: this key is used to allow the extension to specify if JavaScript\n    and CSS files should be injected into all frames matching the specified URL\n    requirements or only into the topmost frame in a tab.\n 3. js: here we specify the script we want to run as a content script. You can\n    specify more than one script, so the value has to be an array\n\nNow we’re ready to test our content script.\n\nBut before we do that we need to do one small step. In order to inform chrome\nthat we updated our chrome extension, we need to go back to chrome://extensions,\ngo to our extension, and press the reload icon.\n\nTo test if our content script is working, go to any web page you want, reload\nthe page if it was opened before, then right-click and press Inspect. In\nDevTools, open the Console tab. You will see in your console as expected:\n\nConsole of a web pageSo, we can verify that our content script is working. Try\nreloading the page, and the content script will execute again. Try opening\nanother web page, and the content script will execute on that page\nindependently.\n\nNow, we know how to inject a script on any web page, and through that script we\ncan do our work.\n\nIn our case, we need to replace every image in any website with random Pikachu\nimages. To do that, we will use this API\n[https://some-random-api.ml/img/pikachu] which will provide us with a JSON\nobject containing the link of a Pikachu image. Each request to that API will\nlook something like this:\n\nResult of API callWith the help of this API, the steps to achieve what we need\nwill be as follows:\n\n 1. Get all image elements on the web page.\n 2. Perform a GET request to the API to retrieve a random Pikachu image link.\n 3. Replace the image elements’ src attributes with the links we retrieve.\n\nThis all sounds good and easy, but there is one complication: content scripts in\nChrome extensions cannot perform Cross-Origin requests.\n\nSo how can we fetch the image URLs from the API? We will need to use a\nBackground script.\n\nAs we specified earlier, Background scripts run in the background in Chrome.\nThey are not dependent on any web page.\n\nScripts in Chrome extensions are isolated from each other. However, they share a\nmean of communication through the Chrome API\n[https://developer.chrome.com/extensions/api_index].\n\nWhat we will do is that we will listen for a message in the background script\nsent from content scripts. Every time a web page opens, our content script will\nrun, send a message to the background script requesting an image URL. The\nbackground script will then perform the asynchronous call to the Pikachu API,\nretrieve the link and send it back to the content script.\n\nThis may sound complicated at first, but it’s actually done in a very simple\nmanner.\n\nLet us first start by creating the background script and see it in action.\nCreate assets/js/background.js with the following content:\n\nNow, we need to tell Chrome that the file we just created will be our background\nscript. In order to do that, we need to go back to our manifest.json and add the\nfollowing:\n\nHere is what everything means:\n\n 1. scripts: An array that holds the paths of the scripts we will use as\n    background scripts. In our case, it’s just one.\n 2. persistent: should always be false unless the extension uses Chrome’s Web\n    Request API to block or modify network requests.\n\nNow, Chrome will know about our background script. It will load it when it’s\nneeded, and unload it when it becomes idle.\n\nTo test our background script, we need to reload our extension as we did\nearlier.\n\nOnce you do that, you will see a new link appear in your extension’s info box\nwith the text “inspect views background page”:\n\nIf you click on it, a new DevTool will open and in the console you will see:\n\nWe can now confirm that our background script is working. Even if you close any\nother web page or reload any web page, nothing will change in the background\nscript.\n\nLet’s go back to our objective again. We now need to listen to messages in our\nbackground script, and on receiving a new message fetch the URL from the Pikachu\nAPI.\n\nIn order to listen to messages, we will use the following method in Chrome’s\nAPI:\n\nWe need to pass a function to addListener. This function will be executed on\nreceiving any new message. The function takes three parameters:\n\n 1. message: the message sent of type any.\n 2. sender: of type MessageSender\n    [https://developer.chrome.com/extensions/runtime#type-MessageSender]\n 3. senderResponse: Callback function to call (at most once) when you have a\n    response. This function is sent by the sender on sending the message.\n\nSo this is what our background.js file should look like now:\n\nNow our background script is ready to receive messages, but there are no\nmessages being sent.\n\nAs we said earlier, each time a page opens, our content script will loop through\nimage elements in the web page, and send a message to the background script to\nfetch the Pikachu image link.\n\nIn order to send messages, we will need to use the following method from\nChrome’s API:\n\nThe parameter’s being sent are as follows:\n\n 1. extensionId: (Optional) the ID of the extension we are sending the message\n    to. If it’s omitted, the message will be sent to our own extension.\n 2. message: The message we are sending of type any.\n 3. options: Optional\n 4. responseCallback: The function to call after receiving the message. This is\n    the senderResponse parameter in the previous method we saw.\n\nSo, we will use this method in our content script to send a message to the\nbackground script to fetch the Pikachu URL from the API. Our content script\nshould now look like this:\n\nHere’s what we’re doing in the above code:\n\n 1. Line 1: get all image elements on the web page.\n 2. Line 2–6: Loop through the image elements. On each element, we send a\n    message to the background script. As you can tell, we are omitting the\n    optional extensionId and options parameters. The first parameter is the \n    message we are sending, and the second parameter is the callback function we\n    want the background to execute after it fetches the image’s link.\n\nWe are sending the object {msg: ‘image’, index: i} to our extension. This object\nhas two properties. The first one is to specify what our message is about. In\nour extension, it might not be very helpful, but when you have an extension that\nuses different types of messages, it’s a good idea to differentiate what each\nmessage is for. The second property is to specify the index of the image the\nlink is for in the images array. The reason behind this is that the callback\nfunction replacing the image will be called asynchronously, so i might point at\nanother index at the time it’s being executed.\n\nThe callback function receives an object that has two properties. data will be\nthe data received from the API call, and index will be the index of the image in\nthe array images. Once it’s called, it will set the image at index in images to\nthe link property is data.\n\nNow, we need to fetch the data from the API in our background script whenever\nthe content script sends a message. We can do this as follows:\n\nHere’s what’s going on here:\n\n 1. Line 1: Listen to messages in our extension\n 2. Line 2: Check if the message is to get the image URL. This will use the msg \n    attribute we passed when we sent the message in contentScript\n 3. Line 3–8: If the condition in Line 2 is true, fetch the data from the API.\n    In the first Promise we get the response text, then in the second Promise we\n    parse the JSON object and call senderResponse which is the callback function\n    specified by the sender of the message. We pass with it an object with the\n    parameters data (the data received through the API call) and index (the\n    index of the image, sent by the sender).\n 4. Line 9: simple handling of any error that might occur.\n 5. Line 10: If the sender specifies a callback function, it has to be called\n    before the listener stops executing or else it will throw an error. In our\n    case, we have to wait until we receive a response from the API which will\n    happen asynchronously, so we return true to specify that it will be called\n    asynchronously.\n\nOkay, our scripts are ready. Only one thing left — we need to give permission to\nthe API in our manifest.json:\n\nAll you need to do now is reload the extension in chrome://extensions, then open\nany page and see Pikachu everywhere!\n\nOf course, this extension will not replace background images. This can be done\nby looping through elements, checking if they have background images, and\nreplacing them the same way.","html":"<p><em>This article was also published on <a href=\"https://levelup.gitconnected.com/chrome-extension-tutorial-replace-images-in-any-website-with-pikachu-de2a6e3548bb\">Level Up Coding's gitconnected</a></em></p><p>What’s a better way to learn how to make a Chrome extension than making the web cuter with Pikachu images?</p><p>In this tutorial, I will show you how you can create a simple Chrome extension that replaces images on websites with Pikachu images. We will discuss topics like content scripts, background scripts, and messages between scripts in Chrome extensions.</p><p>The code for this tutorial is available <a href=\"https://github.com/shahednasser/pikachu-everywhere\" rel=\"noopener\">here</a>.</p><p>First thing you need to do when creating any chrome extension is start with creating a <strong><strong>manifest.json </strong></strong>file in the root of the extension’s directory. For now, this file will hold only the following basic information about your extension:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/6beec3aa60a2187e99e85479f8806c30.js\"></script><!--kg-card-end: html--><p>Here is what everything in this file means:</p><ol><li><strong><strong>name:</strong></strong> The name of the extension. I named the extension “Pikachu Everywhere”</li><li><strong><strong>version</strong></strong>: This is the version of the extension. When you publish the extension in the Chrome Web Store and you need to update it, you will have to increase the version number so it’s good to start with a low number.</li><li><strong><strong>description: (Optional) </strong></strong>Description of your extension</li><li><strong><strong>manifest_version</strong></strong>: As of Chrome 18, developers are required to use manifest version 2, since manifest version 1 is now deprecated.</li><li><strong><strong>icons: (Optional) </strong></strong>Here we specify the extension’s icon. This icon will show up in your browser’s toolbar, the Chrome Web Store, and any other place that requires to show it. For that reason, it’s good to include different sizes of the icon. I have used an icon from <a href=\"https://www.freeiconspng.com/img/17360\" rel=\"noopener\">Free Icons PNG</a> and resized the icons using <a href=\"https://resizeimage.net/\" rel=\"noopener\">https://resizeimage.net/</a> (You can find the images in the <a href=\"https://github.com/shahednasser/pikachu-everywhere\" rel=\"noopener\">GitHub repository</a>). The paths specified for these images is relative to the root of the extension.</li></ol><p>With this, we have the core of any Chrome extension. Next, we’ll add the extension to the browser.</p><p>Go to chrome://extensions. There, you can toggle developer mode at the top right corner. Once you do that, a new toolbar will show up with three buttons: “Load unpacked,” “Pack extension,” and “Update.” Click on “Load unpacked” and choose your extension’s directory.</p><p>Once you do that, you will see that your new chrome extension is now working. Yes, it does nothing, but it’s working.</p><p>In Chrome extensions, there are two types of scripts you can use. There are Background scripts and Content scripts. Background scripts run (obviously) in the background. They are loaded when needed and unloaded when idle. They are used to listen to certain events in your Chrome extension.</p><p>Content scripts are scripts loaded once you open a website and injected it into it. Once you open a website, they will start running just like any other script on that website. After you close the website, the content scripts on that website will stop running as well.</p><p>For our extension, we want to replace every image on every website with Pikachu images. In order to do that we need to specify a content script that works every time you open a web page.</p><p>Let’s create our content script in <code>assets/js/contentScript.js</code> and include the following script to test whether our extension is working or not:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/342ba17b0b6d715adaafd334a14416f9.js\"></script><!--kg-card-end: html--><p>One other thing we need to do is tell Chrome which script do we need to run as a content script. Add the following lines to <strong><strong>manifest.json</strong></strong></p><figure class=\"kg-card kg-embed-card\"><iframe src=\"https://levelup.gitconnected.com/media/d35d7df4566f532c2a0948ea540ee302\" allowfullscreen=\"\" frameborder=\"0\" height=\"0\" width=\"0\" title=\"adding content script to manifest.json\" class=\"t u v hm aj\" scrolling=\"auto\" style=\"box-sizing: inherit; position: absolute; top: 0px; left: 0px; width: 680px; height: 0px;\"></iframe></figure><p>Here we are using <code>content_scripts</code> as key with the value as an array. Here’s what everything in the array means:</p><ol><li><strong><strong>matches</strong></strong>: here you can specify which URLs the content script should run on. In our case we want our content script to run on all pages. So, we use the placeholder “&lt;all_urls&gt;” to specify that.</li><li><strong><strong>all_frames</strong></strong>: this key is used to allow the extension to specify if JavaScript and CSS files should be injected into all frames matching the specified URL requirements or only into the topmost frame in a tab.</li><li><strong><strong>js: </strong></strong>here we specify the script we want to run as a content script. You can specify more than one script, so the value has to be an array</li></ol><p>Now we’re ready to test our content script.</p><p>But before we do that we need to do one small step. In order to inform chrome that we updated our chrome extension, we need to go back to chrome://extensions, go to our extension, and press the reload icon.</p><p>To test if our content script is working, go to any web page you want, reload the page if it was opened before, then right-click and press Inspect. In DevTools, open the Console tab. You will see in your console as expected:</p><figure class=\"kg-card kg-image-card kg-card-hascaption\"><img src=\"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-1.png\" class=\"kg-image\" alt loading=\"lazy\"><figcaption>Console of a web page</figcaption></figure><p>So, we can verify that our content script is working. Try reloading the page, and the content script will execute again. Try opening another web page, and the content script will execute on that page independently.</p><p>Now, we know how to inject a script on any web page, and through that script we can do our work.</p><p>In our case, we need to replace every image in any website with random Pikachu images. To do that, we will use <a href=\"https://some-random-api.ml/img/pikachu\" rel=\"noopener\">this API</a> which will provide us with a JSON object containing the link of a Pikachu image. Each request to that API will look something like this:</p><figure class=\"kg-card kg-image-card kg-card-hascaption\"><img src=\"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-2.png\" class=\"kg-image\" alt loading=\"lazy\"><figcaption>Result of API call</figcaption></figure><p>With the help of this API, the steps to achieve what we need will be as follows:</p><ol><li>Get all image elements on the web page.</li><li>Perform a GET request to the API to retrieve a random Pikachu image link.</li><li>Replace the image elements’ <code>src</code> attributes with the links we retrieve.</li></ol><p>This all sounds good and easy, but there is one complication: content scripts in Chrome extensions cannot perform Cross-Origin requests.</p><p>So how can we fetch the image URLs from the API? We will need to use a Background script.</p><p>As we specified earlier, Background scripts run in the background in Chrome. They are not dependent on any web page.</p><p>Scripts in Chrome extensions are isolated from each other. However, they share a mean of communication through the <a href=\"https://developer.chrome.com/extensions/api_index\" rel=\"noopener\">Chrome API</a>.</p><p>What we will do is that we will listen for a message in the <strong><strong>background</strong></strong> script sent from <strong><strong>content</strong></strong> scripts. Every time a web page opens, our content script will run, send a message to the background script requesting an image URL. The background script will then perform the asynchronous call to the Pikachu API, retrieve the link and send it back to the content script.</p><p>This may sound complicated at first, but it’s actually done in a very simple manner.</p><p>Let us first start by creating the background script and see it in action. Create <code>assets/js/background.js</code> with the following content:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/b45de97a11eda346d1ea427b88c406f4.js\"></script><!--kg-card-end: html--><p>Now, we need to tell Chrome that the file we just created will be our background script. In order to do that, we need to go back to our <strong><strong>manifest.json</strong></strong> and add the following:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/c7a26cf5b3b2415110cb97e44f2b3834.js\"></script><!--kg-card-end: html--><p>Here is what everything means:</p><ol><li><strong><strong>scripts: </strong></strong>An array that holds the paths of the scripts we will use as background scripts. In our case, it’s just one.</li><li><strong><strong>persistent</strong></strong>: should always be false unless the extension uses Chrome’s Web Request API to block or modify network requests.</li></ol><p>Now, Chrome will know about our background script. It will load it when it’s needed, and unload it when it becomes idle.</p><p>To test our background script, we need to reload our extension as we did earlier.</p><p>Once you do that, you will see a new link appear in your extension’s info box with the text “inspect views background page”:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-3.png\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>If you click on it, a new DevTool will open and in the console you will see:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-4.png\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>We can now confirm that our background script is working. Even if you close any other web page or reload any web page, nothing will change in the background script.</p><p>Let’s go back to our objective again. We now need to listen to messages in our background script, and on receiving a new message fetch the URL from the Pikachu API.</p><p>In order to listen to messages, we will use the following method in Chrome’s API:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/4426ca477880552e4c563be8264b968c.js\"></script><!--kg-card-end: html--><p>We need to pass a function to <code>addListener</code>. This function will be executed on receiving any new message. The function takes three parameters:</p><ol><li><strong><strong>message: </strong></strong>the message sent of type any.</li><li><strong><strong>sender: </strong></strong>of type <a href=\"https://developer.chrome.com/extensions/runtime#type-MessageSender\" rel=\"noopener\">MessageSender</a></li><li><strong><strong>senderResponse: </strong></strong>Callback function to call (at most once) when you have a response. This function is sent by the sender on sending the message.</li></ol><p>So this is what our <strong><strong>background.js </strong></strong>file should look like now:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/4920b314274d2695b5ac0e771a4cd52c.js\"></script><!--kg-card-end: html--><p>Now our background script is ready to receive messages, but there are no messages being sent.</p><p>As we said earlier, each time a page opens, our content script will loop through image elements in the web page, and send a message to the background script to fetch the Pikachu image link.</p><p>In order to send messages, we will need to use the following method from Chrome’s API:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/03e371015982ef271518922e56139c19.js\"></script><!--kg-card-end: html--><p>The parameter’s being sent are as follows:</p><ol><li><strong><strong>extensionId: (Optional) </strong></strong>the ID of the extension we are sending the message to. If it’s omitted, the message will be sent to our own extension.</li><li><strong><strong>message:</strong></strong> The message we are sending of type any.</li><li><strong><strong>options: Optional</strong></strong></li><li><strong><strong>responseCallback: </strong></strong>The function to call after receiving the message. This is the <strong><strong>senderResponse</strong></strong> parameter in the previous method we saw.</li></ol><p>So, we will use this method in our content script to send a message to the background script to fetch the Pikachu URL from the API. Our content script should now look like this:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/bd4051325006a5f3ca01e882b51eebc5.js\"></script><!--kg-card-end: html--><p>Here’s what we’re doing in the above code:</p><ol><li><strong><strong>Line 1: </strong></strong>get all image elements on the web page.</li><li><strong><strong>Line 2–6: </strong></strong>Loop through the image elements. On each element, we send a message to the background script. As you can tell, we are omitting the optional <strong><strong>extensionId </strong></strong>and <strong><strong>options</strong></strong> parameters. The first parameter is the <strong><strong>message </strong></strong>we are sending, and the second parameter is the callback function we want the background to execute after it fetches the image’s link.</li></ol><p>We are sending the object <strong><strong>{msg: ‘image’, index: i}</strong></strong> to our extension. This object has two properties. The first one is to specify what our message is about. In our extension, it might not be very helpful, but when you have an extension that uses different types of messages, it’s a good idea to differentiate what each message is for. The second property is to specify the index of the image the link is for in the <strong><strong>images</strong></strong> array. The reason behind this is that the callback function replacing the image will be called asynchronously, so <strong><strong>i </strong></strong>might point at another index at the time it’s being executed.</p><p>The callback function receives an object that has two properties. <strong><strong>data</strong></strong> will be the data received from the API call, and <strong><strong>index</strong></strong> will be the index of the image in the array <strong><strong>images</strong></strong>. Once it’s called, it will set the image at <strong><strong>index</strong></strong> in <strong><strong>images</strong></strong> to the link property is <strong><strong>data</strong></strong>.</p><p>Now, we need to fetch the data from the API in our background script whenever the content script sends a message. We can do this as follows:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/9c024a8b40396e7c6a6ae1250fb5d94d.js\"></script><!--kg-card-end: html--><p>Here’s what’s going on here:</p><ol><li><strong><strong>Line 1:</strong></strong> Listen to messages in our extension</li><li><strong><strong>Line 2: </strong></strong>Check if the message is to get the image URL. This will use the <strong><strong>msg </strong></strong>attribute we passed when we sent the message in <strong><strong>contentScript</strong></strong></li><li><strong><strong>Line 3–8:</strong></strong> If the condition in <strong><strong>Line 2</strong></strong> is true, fetch the data from the API. In the first <code>Promise</code> we get the response text, then in the second <code>Promise</code> we parse the JSON object and call <strong><strong>senderResponse</strong></strong> which is the callback function specified by the sender of the message. We pass with it an object with the parameters <strong><strong>data </strong></strong>(the data received through the API call) and <strong><strong>index</strong></strong> (the index of the image, sent by the sender).</li><li><strong><strong>Line 9: </strong></strong>simple handling of any error that might occur.</li><li><strong><strong>Line 10</strong></strong>: If the sender specifies a callback function, it has to be called before the listener stops executing or else it will throw an error. In our case, we have to wait until we receive a response from the API which will happen asynchronously, so we return true to specify that it will be called asynchronously.</li></ol><p>Okay, our scripts are ready. Only one thing left — we need to give permission to the API in our <strong><strong>manifest.json:</strong></strong></p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/8d6786f1583a91fe31635715caf44b0c.js\"></script><!--kg-card-end: html--><p>All you need to do now is reload the extension in chrome://extensions, then open any page and see Pikachu everywhere!</p><p>Of course, this extension will not replace background images. This can be done by looping through elements, checking if they have background images, and replacing them the same way.</p>","url":"https://backend.shahednasser.com/chrome-extension-tutorial-replace-images-in-any-website-with-pikachu/","canonical_url":null,"uuid":"d3c062c3-8b05-4b63-919c-4fbbcdb59cc3","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5fa596aabc1317001e675bff","reading_time":9,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p><em>This article was also published on <a href=\"https://levelup.gitconnected.com/chrome-extension-tutorial-replace-images-in-any-website-with-pikachu-de2a6e3548bb\">Level Up Coding's gitconnected</a></em></p><p>What’s a better way to learn how to make a Chrome extension than making the web cuter with Pikachu images?</p><p>In this tutorial, I will show you how you can create a simple Chrome extension that replaces images on websites with Pikachu images. We will discuss topics like content scripts, background scripts, and messages between scripts in Chrome extensions.</p><p>The code for this tutorial is available <a href=\"https://github.com/shahednasser/pikachu-everywhere\" rel=\"noopener\">here</a>.</p><p>First thing you need to do when creating any chrome extension is start with creating a <strong><strong>manifest.json </strong></strong>file in the root of the extension’s directory. For now, this file will hold only the following basic information about your extension:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/6beec3aa60a2187e99e85479f8806c30.js\"></script><!--kg-card-end: html--><p>Here is what everything in this file means:</p><ol><li><strong><strong>name:</strong></strong> The name of the extension. I named the extension “Pikachu Everywhere”</li><li><strong><strong>version</strong></strong>: This is the version of the extension. When you publish the extension in the Chrome Web Store and you need to update it, you will have to increase the version number so it’s good to start with a low number.</li><li><strong><strong>description: (Optional) </strong></strong>Description of your extension</li><li><strong><strong>manifest_version</strong></strong>: As of Chrome 18, developers are required to use manifest version 2, since manifest version 1 is now deprecated.</li><li><strong><strong>icons: (Optional) </strong></strong>Here we specify the extension’s icon. This icon will show up in your browser’s toolbar, the Chrome Web Store, and any other place that requires to show it. For that reason, it’s good to include different sizes of the icon. I have used an icon from <a href=\"https://www.freeiconspng.com/img/17360\" rel=\"noopener\">Free Icons PNG</a> and resized the icons using <a href=\"https://resizeimage.net/\" rel=\"noopener\">https://resizeimage.net/</a> (You can find the images in the <a href=\"https://github.com/shahednasser/pikachu-everywhere\" rel=\"noopener\">GitHub repository</a>). The paths specified for these images is relative to the root of the extension.</li></ol><p>With this, we have the core of any Chrome extension. Next, we’ll add the extension to the browser.</p><p>Go to chrome://extensions. There, you can toggle developer mode at the top right corner. Once you do that, a new toolbar will show up with three buttons: “Load unpacked,” “Pack extension,” and “Update.” Click on “Load unpacked” and choose your extension’s directory.</p><p>Once you do that, you will see that your new chrome extension is now working. Yes, it does nothing, but it’s working.</p><p>In Chrome extensions, there are two types of scripts you can use. There are Background scripts and Content scripts. Background scripts run (obviously) in the background. They are loaded when needed and unloaded when idle. They are used to listen to certain events in your Chrome extension.</p><p>Content scripts are scripts loaded once you open a website and injected it into it. Once you open a website, they will start running just like any other script on that website. After you close the website, the content scripts on that website will stop running as well.</p><p>For our extension, we want to replace every image on every website with Pikachu images. In order to do that we need to specify a content script that works every time you open a web page.</p><p>Let’s create our content script in <code class=\"language-text\">assets/js/contentScript.js</code> and include the following script to test whether our extension is working or not:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/342ba17b0b6d715adaafd334a14416f9.js\"></script><!--kg-card-end: html--><p>One other thing we need to do is tell Chrome which script do we need to run as a content script. Add the following lines to <strong><strong>manifest.json</strong></strong></p><figure class=\"kg-card kg-embed-card\"><iframe src=\"https://levelup.gitconnected.com/media/d35d7df4566f532c2a0948ea540ee302\" allowfullscreen frameborder=\"0\" height=\"0\" width=\"0\" title=\"adding content script to manifest.json\" class=\"t u v hm aj\" scrolling=\"auto\" style=\"box-sizing: inherit; position: absolute; top: 0px; left: 0px; width: 680px; height: 0px;\"></iframe></figure><p>Here we are using <code class=\"language-text\">content_scripts</code> as key with the value as an array. Here’s what everything in the array means:</p><ol><li><strong><strong>matches</strong></strong>: here you can specify which URLs the content script should run on. In our case we want our content script to run on all pages. So, we use the placeholder “&#x3C;all_urls>” to specify that.</li><li><strong><strong>all_frames</strong></strong>: this key is used to allow the extension to specify if JavaScript and CSS files should be injected into all frames matching the specified URL requirements or only into the topmost frame in a tab.</li><li><strong><strong>js: </strong></strong>here we specify the script we want to run as a content script. You can specify more than one script, so the value has to be an array</li></ol><p>Now we’re ready to test our content script.</p><p>But before we do that we need to do one small step. In order to inform chrome that we updated our chrome extension, we need to go back to chrome://extensions, go to our extension, and press the reload icon.</p><p>To test if our content script is working, go to any web page you want, reload the page if it was opened before, then right-click and press Inspect. In DevTools, open the Console tab. You will see in your console as expected:</p><figure class=\"kg-card kg-image-card kg-card-hascaption\"><img src=\"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\"><figcaption>Console of a web page</figcaption></figure><p>So, we can verify that our content script is working. Try reloading the page, and the content script will execute again. Try opening another web page, and the content script will execute on that page independently.</p><p>Now, we know how to inject a script on any web page, and through that script we can do our work.</p><p>In our case, we need to replace every image in any website with random Pikachu images. To do that, we will use <a href=\"https://some-random-api.ml/img/pikachu\" rel=\"noopener\">this API</a> which will provide us with a JSON object containing the link of a Pikachu image. Each request to that API will look something like this:</p><figure class=\"kg-card kg-image-card kg-card-hascaption\"><img src=\"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-2.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\"><figcaption>Result of API call</figcaption></figure><p>With the help of this API, the steps to achieve what we need will be as follows:</p><ol><li>Get all image elements on the web page.</li><li>Perform a GET request to the API to retrieve a random Pikachu image link.</li><li>Replace the image elements’ <code class=\"language-text\">src</code> attributes with the links we retrieve.</li></ol><p>This all sounds good and easy, but there is one complication: content scripts in Chrome extensions cannot perform Cross-Origin requests.</p><p>So how can we fetch the image URLs from the API? We will need to use a Background script.</p><p>As we specified earlier, Background scripts run in the background in Chrome. They are not dependent on any web page.</p><p>Scripts in Chrome extensions are isolated from each other. However, they share a mean of communication through the <a href=\"https://developer.chrome.com/extensions/api_index\" rel=\"noopener\">Chrome API</a>.</p><p>What we will do is that we will listen for a message in the <strong><strong>background</strong></strong> script sent from <strong><strong>content</strong></strong> scripts. Every time a web page opens, our content script will run, send a message to the background script requesting an image URL. The background script will then perform the asynchronous call to the Pikachu API, retrieve the link and send it back to the content script.</p><p>This may sound complicated at first, but it’s actually done in a very simple manner.</p><p>Let us first start by creating the background script and see it in action. Create <code class=\"language-text\">assets/js/background.js</code> with the following content:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/b45de97a11eda346d1ea427b88c406f4.js\"></script><!--kg-card-end: html--><p>Now, we need to tell Chrome that the file we just created will be our background script. In order to do that, we need to go back to our <strong><strong>manifest.json</strong></strong> and add the following:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/c7a26cf5b3b2415110cb97e44f2b3834.js\"></script><!--kg-card-end: html--><p>Here is what everything means:</p><ol><li><strong><strong>scripts: </strong></strong>An array that holds the paths of the scripts we will use as background scripts. In our case, it’s just one.</li><li><strong><strong>persistent</strong></strong>: should always be false unless the extension uses Chrome’s Web Request API to block or modify network requests.</li></ol><p>Now, Chrome will know about our background script. It will load it when it’s needed, and unload it when it becomes idle.</p><p>To test our background script, we need to reload our extension as we did earlier.</p><p>Once you do that, you will see a new link appear in your extension’s info box with the text “inspect views background page”:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-5.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-3.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>If you click on it, a new DevTool will open and in the console you will see:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-4.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-4.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>We can now confirm that our background script is working. Even if you close any other web page or reload any web page, nothing will change in the background script.</p><p>Let’s go back to our objective again. We now need to listen to messages in our background script, and on receiving a new message fetch the URL from the Pikachu API.</p><p>In order to listen to messages, we will use the following method in Chrome’s API:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/4426ca477880552e4c563be8264b968c.js\"></script><!--kg-card-end: html--><p>We need to pass a function to <code class=\"language-text\">addListener</code>. This function will be executed on receiving any new message. The function takes three parameters:</p><ol><li><strong><strong>message: </strong></strong>the message sent of type any.</li><li><strong><strong>sender: </strong></strong>of type <a href=\"https://developer.chrome.com/extensions/runtime#type-MessageSender\" rel=\"noopener\">MessageSender</a></li><li><strong><strong>senderResponse: </strong></strong>Callback function to call (at most once) when you have a response. This function is sent by the sender on sending the message.</li></ol><p>So this is what our <strong><strong>background.js </strong></strong>file should look like now:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/4920b314274d2695b5ac0e771a4cd52c.js\"></script><!--kg-card-end: html--><p>Now our background script is ready to receive messages, but there are no messages being sent.</p><p>As we said earlier, each time a page opens, our content script will loop through image elements in the web page, and send a message to the background script to fetch the Pikachu image link.</p><p>In order to send messages, we will need to use the following method from Chrome’s API:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/03e371015982ef271518922e56139c19.js\"></script><!--kg-card-end: html--><p>The parameter’s being sent are as follows:</p><ol><li><strong><strong>extensionId: (Optional) </strong></strong>the ID of the extension we are sending the message to. If it’s omitted, the message will be sent to our own extension.</li><li><strong><strong>message:</strong></strong> The message we are sending of type any.</li><li><strong><strong>options: Optional</strong></strong></li><li><strong><strong>responseCallback: </strong></strong>The function to call after receiving the message. This is the <strong><strong>senderResponse</strong></strong> parameter in the previous method we saw.</li></ol><p>So, we will use this method in our content script to send a message to the background script to fetch the Pikachu URL from the API. Our content script should now look like this:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/bd4051325006a5f3ca01e882b51eebc5.js\"></script><!--kg-card-end: html--><p>Here’s what we’re doing in the above code:</p><ol><li><strong><strong>Line 1: </strong></strong>get all image elements on the web page.</li><li><strong><strong>Line 2–6: </strong></strong>Loop through the image elements. On each element, we send a message to the background script. As you can tell, we are omitting the optional <strong><strong>extensionId </strong></strong>and <strong><strong>options</strong></strong> parameters. The first parameter is the <strong><strong>message </strong></strong>we are sending, and the second parameter is the callback function we want the background to execute after it fetches the image’s link.</li></ol><p>We are sending the object <strong><strong>{msg: ‘image’, index: i}</strong></strong> to our extension. This object has two properties. The first one is to specify what our message is about. In our extension, it might not be very helpful, but when you have an extension that uses different types of messages, it’s a good idea to differentiate what each message is for. The second property is to specify the index of the image the link is for in the <strong><strong>images</strong></strong> array. The reason behind this is that the callback function replacing the image will be called asynchronously, so <strong><strong>i </strong></strong>might point at another index at the time it’s being executed.</p><p>The callback function receives an object that has two properties. <strong><strong>data</strong></strong> will be the data received from the API call, and <strong><strong>index</strong></strong> will be the index of the image in the array <strong><strong>images</strong></strong>. Once it’s called, it will set the image at <strong><strong>index</strong></strong> in <strong><strong>images</strong></strong> to the link property is <strong><strong>data</strong></strong>.</p><p>Now, we need to fetch the data from the API in our background script whenever the content script sends a message. We can do this as follows:</p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/9c024a8b40396e7c6a6ae1250fb5d94d.js\"></script><!--kg-card-end: html--><p>Here’s what’s going on here:</p><ol><li><strong><strong>Line 1:</strong></strong> Listen to messages in our extension</li><li><strong><strong>Line 2: </strong></strong>Check if the message is to get the image URL. This will use the <strong><strong>msg </strong></strong>attribute we passed when we sent the message in <strong><strong>contentScript</strong></strong></li><li><strong><strong>Line 3–8:</strong></strong> If the condition in <strong><strong>Line 2</strong></strong> is true, fetch the data from the API. In the first <code class=\"language-text\">Promise</code> we get the response text, then in the second <code class=\"language-text\">Promise</code> we parse the JSON object and call <strong><strong>senderResponse</strong></strong> which is the callback function specified by the sender of the message. We pass with it an object with the parameters <strong><strong>data </strong></strong>(the data received through the API call) and <strong><strong>index</strong></strong> (the index of the image, sent by the sender).</li><li><strong><strong>Line 9: </strong></strong>simple handling of any error that might occur.</li><li><strong><strong>Line 10</strong></strong>: If the sender specifies a callback function, it has to be called before the listener stops executing or else it will throw an error. In our case, we have to wait until we receive a response from the API which will happen asynchronously, so we return true to specify that it will be called asynchronously.</li></ol><p>Okay, our scripts are ready. Only one thing left — we need to give permission to the API in our <strong><strong>manifest.json:</strong></strong></p><!--kg-card-begin: html--><script src=\"https://gist.github.com/shahednasser/8d6786f1583a91fe31635715caf44b0c.js\"></script><!--kg-card-end: html--><p>All you need to do now is reload the extension in chrome://extensions, then open any page and see Pikachu everywhere!</p><p>Of course, this extension will not replace background images. This can be done by looping through elements, checking if they have background images, and replacing them the same way.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"This article was also published on "},{"type":"element","tagName":"a","properties":{"href":"https://levelup.gitconnected.com/chrome-extension-tutorial-replace-images-in-any-website-with-pikachu-de2a6e3548bb"},"children":[{"type":"text","value":"Level Up Coding's gitconnected"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What’s a better way to learn how to make a Chrome extension than making the web cuter with Pikachu images?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, I will show you how you can create a simple Chrome extension that replaces images on websites with Pikachu images. We will discuss topics like content scripts, background scripts, and messages between scripts in Chrome extensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code for this tutorial is available "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/pikachu-everywhere","rel":["noopener"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First thing you need to do when creating any chrome extension is start with creating a "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"manifest.json "}]}]},{"type":"text","value":"file in the root of the extension’s directory. For now, this file will hold only the following basic information about your extension:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/6beec3aa60a2187e99e85479f8806c30.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is what everything in this file means:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"name:"}]}]},{"type":"text","value":" The name of the extension. I named the extension “Pikachu Everywhere”"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"version"}]}]},{"type":"text","value":": This is the version of the extension. When you publish the extension in the Chrome Web Store and you need to update it, you will have to increase the version number so it’s good to start with a low number."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"description: (Optional) "}]}]},{"type":"text","value":"Description of your extension"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"manifest_version"}]}]},{"type":"text","value":": As of Chrome 18, developers are required to use manifest version 2, since manifest version 1 is now deprecated."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"icons: (Optional) "}]}]},{"type":"text","value":"Here we specify the extension’s icon. This icon will show up in your browser’s toolbar, the Chrome Web Store, and any other place that requires to show it. For that reason, it’s good to include different sizes of the icon. I have used an icon from "},{"type":"element","tagName":"a","properties":{"href":"https://www.freeiconspng.com/img/17360","rel":["noopener"]},"children":[{"type":"text","value":"Free Icons PNG"}]},{"type":"text","value":" and resized the icons using "},{"type":"element","tagName":"a","properties":{"href":"https://resizeimage.net/","rel":["noopener"]},"children":[{"type":"text","value":"https://resizeimage.net/"}]},{"type":"text","value":" (You can find the images in the "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/pikachu-everywhere","rel":["noopener"]},"children":[{"type":"text","value":"GitHub repository"}]},{"type":"text","value":"). The paths specified for these images is relative to the root of the extension."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With this, we have the core of any Chrome extension. Next, we’ll add the extension to the browser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Go to chrome://extensions. There, you can toggle developer mode at the top right corner. Once you do that, a new toolbar will show up with three buttons: “Load unpacked,” “Pack extension,” and “Update.” Click on “Load unpacked” and choose your extension’s directory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you do that, you will see that your new chrome extension is now working. Yes, it does nothing, but it’s working."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Chrome extensions, there are two types of scripts you can use. There are Background scripts and Content scripts. Background scripts run (obviously) in the background. They are loaded when needed and unloaded when idle. They are used to listen to certain events in your Chrome extension."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Content scripts are scripts loaded once you open a website and injected it into it. Once you open a website, they will start running just like any other script on that website. After you close the website, the content scripts on that website will stop running as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For our extension, we want to replace every image on every website with Pikachu images. In order to do that we need to specify a content script that works every time you open a web page."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let’s create our content script in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"assets/js/contentScript.js"}]},{"type":"text","value":" and include the following script to test whether our extension is working or not:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/342ba17b0b6d715adaafd334a14416f9.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One other thing we need to do is tell Chrome which script do we need to run as a content script. Add the following lines to "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"manifest.json"}]}]}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-embed-card"]},"children":[{"type":"element","tagName":"iframe","properties":{"src":"https://levelup.gitconnected.com/media/d35d7df4566f532c2a0948ea540ee302","allowFullScreen":true,"frameBorder":"0","height":0,"width":0,"title":"adding content script to manifest.json","className":["t","u","v","hm","aj"],"scrolling":"auto","style":"box-sizing: inherit; position: absolute; top: 0px; left: 0px; width: 680px; height: 0px;"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we are using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content_scripts"}]},{"type":"text","value":" as key with the value as an array. Here’s what everything in the array means:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"matches"}]}]},{"type":"text","value":": here you can specify which URLs the content script should run on. In our case we want our content script to run on all pages. So, we use the placeholder “<all_urls>” to specify that."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"all_frames"}]}]},{"type":"text","value":": this key is used to allow the extension to specify if JavaScript and CSS files should be injected into all frames matching the specified URL requirements or only into the topmost frame in a tab."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"js: "}]}]},{"type":"text","value":"here we specify the script we want to run as a content script. You can specify more than one script, so the value has to be an array"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we’re ready to test our content script."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But before we do that we need to do one small step. In order to inform chrome that we updated our chrome extension, we need to go back to chrome://extensions, go to our extension, and press the reload icon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To test if our content script is working, go to any web page you want, reload the page if it was opened before, then right-click and press Inspect. In DevTools, open the Console tab. You will see in your console as expected:"}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card","kg-card-hascaption"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-1.png","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]},{"type":"element","tagName":"figcaption","properties":{},"children":[{"type":"text","value":"Console of a web page"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, we can verify that our content script is working. Try reloading the page, and the content script will execute again. Try opening another web page, and the content script will execute on that page independently."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, we know how to inject a script on any web page, and through that script we can do our work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In our case, we need to replace every image in any website with random Pikachu images. To do that, we will use "},{"type":"element","tagName":"a","properties":{"href":"https://some-random-api.ml/img/pikachu","rel":["noopener"]},"children":[{"type":"text","value":"this API"}]},{"type":"text","value":" which will provide us with a JSON object containing the link of a Pikachu image. Each request to that API will look something like this:"}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card","kg-card-hascaption"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://res-3.cloudinary.com/hbqmf3mbz/image/upload/q_auto/v1/ghost-blog-images/chrome-extension-screenshot-2.png","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]},{"type":"element","tagName":"figcaption","properties":{},"children":[{"type":"text","value":"Result of API call"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the help of this API, the steps to achieve what we need will be as follows:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Get all image elements on the web page."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Perform a GET request to the API to retrieve a random Pikachu image link."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Replace the image elements’ "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src"}]},{"type":"text","value":" attributes with the links we retrieve."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This all sounds good and easy, but there is one complication: content scripts in Chrome extensions cannot perform Cross-Origin requests."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So how can we fetch the image URLs from the API? We will need to use a Background script."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we specified earlier, Background scripts run in the background in Chrome. They are not dependent on any web page."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Scripts in Chrome extensions are isolated from each other. However, they share a mean of communication through the "},{"type":"element","tagName":"a","properties":{"href":"https://developer.chrome.com/extensions/api_index","rel":["noopener"]},"children":[{"type":"text","value":"Chrome API"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What we will do is that we will listen for a message in the "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"background"}]}]},{"type":"text","value":" script sent from "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"content"}]}]},{"type":"text","value":" scripts. Every time a web page opens, our content script will run, send a message to the background script requesting an image URL. The background script will then perform the asynchronous call to the Pikachu API, retrieve the link and send it back to the content script."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This may sound complicated at first, but it’s actually done in a very simple manner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let us first start by creating the background script and see it in action. Create "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"assets/js/background.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/b45de97a11eda346d1ea427b88c406f4.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, we need to tell Chrome that the file we just created will be our background script. In order to do that, we need to go back to our "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"manifest.json"}]}]},{"type":"text","value":" and add the following:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/c7a26cf5b3b2415110cb97e44f2b3834.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is what everything means:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"scripts: "}]}]},{"type":"text","value":"An array that holds the paths of the scripts we will use as background scripts. In our case, it’s just one."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"persistent"}]}]},{"type":"text","value":": should always be false unless the extension uses Chrome’s Web Request API to block or modify network requests."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, Chrome will know about our background script. It will load it when it’s needed, and unload it when it becomes idle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To test our background script, we need to reload our extension as we did earlier."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you do that, you will see a new link appear in your extension’s info box with the text “inspect views background page”:"}]},{"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/chrome-extension-screenshot-3.png","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you click on it, a new DevTool will open and in the console you will see:"}]},{"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/chrome-extension-screenshot-4.png","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can now confirm that our background script is working. Even if you close any other web page or reload any web page, nothing will change in the background script."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let’s go back to our objective again. We now need to listen to messages in our background script, and on receiving a new message fetch the URL from the Pikachu API."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In order to listen to messages, we will use the following method in Chrome’s API:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/4426ca477880552e4c563be8264b968c.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We need to pass a function to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"addListener"}]},{"type":"text","value":". This function will be executed on receiving any new message. The function takes three parameters:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"message: "}]}]},{"type":"text","value":"the message sent of type any."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sender: "}]}]},{"type":"text","value":"of type "},{"type":"element","tagName":"a","properties":{"href":"https://developer.chrome.com/extensions/runtime#type-MessageSender","rel":["noopener"]},"children":[{"type":"text","value":"MessageSender"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"senderResponse: "}]}]},{"type":"text","value":"Callback function to call (at most once) when you have a response. This function is sent by the sender on sending the message."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So this is what our "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"background.js "}]}]},{"type":"text","value":"file should look like now:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/4920b314274d2695b5ac0e771a4cd52c.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now our background script is ready to receive messages, but there are no messages being sent."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we said earlier, each time a page opens, our content script will loop through image elements in the web page, and send a message to the background script to fetch the Pikachu image link."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In order to send messages, we will need to use the following method from Chrome’s API:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/03e371015982ef271518922e56139c19.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The parameter’s being sent are as follows:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"extensionId: (Optional) "}]}]},{"type":"text","value":"the ID of the extension we are sending the message to. If it’s omitted, the message will be sent to our own extension."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"message:"}]}]},{"type":"text","value":" The message we are sending of type any."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"options: Optional"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"responseCallback: "}]}]},{"type":"text","value":"The function to call after receiving the message. This is the "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"senderResponse"}]}]},{"type":"text","value":" parameter in the previous method we saw."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, we will use this method in our content script to send a message to the background script to fetch the Pikachu URL from the API. Our content script should now look like this:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/bd4051325006a5f3ca01e882b51eebc5.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here’s what we’re doing in the above code:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 1: "}]}]},{"type":"text","value":"get all image elements on the web page."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 2–6: "}]}]},{"type":"text","value":"Loop through the image elements. On each element, we send a message to the background script. As you can tell, we are omitting the optional "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"extensionId "}]}]},{"type":"text","value":"and "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"options"}]}]},{"type":"text","value":" parameters. The first parameter is the "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"message "}]}]},{"type":"text","value":"we are sending, and the second parameter is the callback function we want the background to execute after it fetches the image’s link."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We are sending the object "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"{msg: ‘image’, index: i}"}]}]},{"type":"text","value":" to our extension. This object has two properties. The first one is to specify what our message is about. In our extension, it might not be very helpful, but when you have an extension that uses different types of messages, it’s a good idea to differentiate what each message is for. The second property is to specify the index of the image the link is for in the "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"images"}]}]},{"type":"text","value":" array. The reason behind this is that the callback function replacing the image will be called asynchronously, so "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"i "}]}]},{"type":"text","value":"might point at another index at the time it’s being executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The callback function receives an object that has two properties. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"data"}]}]},{"type":"text","value":" will be the data received from the API call, and "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"index"}]}]},{"type":"text","value":" will be the index of the image in the array "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"images"}]}]},{"type":"text","value":". Once it’s called, it will set the image at "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"index"}]}]},{"type":"text","value":" in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"images"}]}]},{"type":"text","value":" to the link property is "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"data"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, we need to fetch the data from the API in our background script whenever the content script sends a message. We can do this as follows:"}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/9c024a8b40396e7c6a6ae1250fb5d94d.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here’s what’s going on here:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 1:"}]}]},{"type":"text","value":" Listen to messages in our extension"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 2: "}]}]},{"type":"text","value":"Check if the message is to get the image URL. This will use the "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"msg "}]}]},{"type":"text","value":"attribute we passed when we sent the message in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"contentScript"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 3–8:"}]}]},{"type":"text","value":" If the condition in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 2"}]}]},{"type":"text","value":" is true, fetch the data from the API. In the first "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Promise"}]},{"type":"text","value":" we get the response text, then in the second "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Promise"}]},{"type":"text","value":" we parse the JSON object and call "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"senderResponse"}]}]},{"type":"text","value":" which is the callback function specified by the sender of the message. We pass with it an object with the parameters "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"data "}]}]},{"type":"text","value":"(the data received through the API call) and "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"index"}]}]},{"type":"text","value":" (the index of the image, sent by the sender)."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 9: "}]}]},{"type":"text","value":"simple handling of any error that might occur."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Line 10"}]}]},{"type":"text","value":": If the sender specifies a callback function, it has to be called before the listener stops executing or else it will throw an error. In our case, we have to wait until we receive a response from the API which will happen asynchronously, so we return true to specify that it will be called asynchronously."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Okay, our scripts are ready. Only one thing left — we need to give permission to the API in our "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"manifest.json:"}]}]}]},{"type":"comment","value":"kg-card-begin: html"},{"type":"element","tagName":"script","properties":{"src":"https://gist.github.com/shahednasser/8d6786f1583a91fe31635715caf44b0c.js"},"children":[]},{"type":"comment","value":"kg-card-end: html"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All you need to do now is reload the extension in chrome://extensions, then open any page and see Pikachu everywhere!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Of course, this extension will not replace background images. This can be done by looping through elements, checking if they have background images, and replacing them the same way."}]}],"data":{"quirksMode":false}},"tableOfContents":[]},"featureImageSharp":{"base":"chrome-extension.gif","publicURL":"/static/498b0f8235a6b5cc5d72fd2244922304/chrome-extension.gif","imageMeta":{"width":500,"height":309},"childImageSharp":null}},"next":null,"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":"website-ideas-to-practice-your-web-development-skills","prev":"chrome-extension-tutorial-replace-images-in-any-website-with-pikachu","next":"","tag":"beginner","limit":3,"skip":0,"primaryTagCount":62,"collectionPaths":{}}},"staticQueryHashes":["1272700106","1676991999","2138873178","2546165603","2681841279","2938721187","293880488","3052966952","4156497161"]}