Vanilla JS LocalStorage Counter - JavaScript Typing CST Test
Loading…
Vanilla JS LocalStorage Counter — JavaScript Code
Counter that persists its value using localStorage.
document.addEventListener('DOMContentLoaded', () => {
const counterEl = document.getElementById('counter');
const incBtn = document.getElementById('increment');
let count = parseInt(localStorage.getItem('count') || '0', 10);
const updateUI = () => {
counterEl.textContent = `Counter: ${count}`;
localStorage.setItem('count', count);
};
incBtn.addEventListener('click', () => { count++; updateUI(); });
updateUI();
});JavaScript Language Guide
JavaScript is a high-level, interpreted programming language primarily used to create dynamic and interactive content on web pages. It enables client-side scripting, DOM manipulation, and integration with HTML and CSS.
Primary Use Cases
- ▸Adding interactivity to web pages
- ▸Manipulating the DOM and HTML elements dynamically
- ▸Form validation and user input handling
- ▸Creating web applications and single-page applications (SPA)
- ▸Server-side development with Node.js
Notable Features
- ▸Event handling and listeners
- ▸Asynchronous programming with Promises and async/await
- ▸Prototype-based object-oriented programming
- ▸Dynamic typing and flexible syntax
- ▸Extensive ecosystem of libraries and frameworks (React, Angular, Vue)
Origin & Creator
Created by Brendan Eich in 1995 at Netscape Communications.
Industrial Note
JavaScript is specialized for web interactivity, dynamic content updates, and client-side logic, but it is also widely used for server-side applications via Node.js.
Quick Explain
- ▸JavaScript allows developers to control the behavior of web pages.
- ▸It supports object-oriented, functional, and event-driven programming paradigms.
- ▸Works alongside HTML for structure and CSS for styling.
Core Features
- ▸First-class functions and closures
- ▸DOM and BOM (Browser Object Model) access
- ▸Cross-platform execution in browsers and servers
- ▸JSON support for data interchange
- ▸Modules for code organization
Learning Path
- ▸Learn syntax, variables, and types
- ▸Understand functions, scope, and closures
- ▸Practice DOM manipulation
- ▸Explore asynchronous programming
- ▸Learn frameworks and Node.js for full-stack
Practical Examples
- ▸Creating a dynamic image slider
- ▸Form validation and error handling
- ▸Single-page application routing
- ▸Fetching data from APIs using fetch/axios
- ▸Building interactive dashboards and charts
Comparisons
- ▸More dynamic than HTML/CSS alone
- ▸Runs client-side unlike server-side languages like PHP
- ▸Complementary with frameworks for SPA
- ▸Event-driven unlike static languages
- ▸Versatile for full-stack development
Strengths
- ▸Runs in all modern browsers
- ▸Large and active developer community
- ▸Rich ecosystem of frameworks and libraries
- ▸Supports both client-side and server-side development
- ▸Enables asynchronous, event-driven programming
Limitations
- ▸Single-threaded, which may impact CPU-intensive tasks
- ▸Browser differences can cause inconsistencies
- ▸Dynamic typing may lead to runtime errors
- ▸Security issues like XSS need careful handling
- ▸Heavy DOM manipulation can reduce performance
When NOT to Use
- ▸For pure static content without interactivity
- ▸Heavy computation without Web Workers
- ▸Server-only tasks without Node.js
- ▸Environments without JS engines
- ▸Real-time low-level system programming
Cheat Sheet
- ▸document.querySelector('#id') - select element
- ▸element.addEventListener('click', fn) - add event listener
- ▸fetch(url).then(res => res.json()) - fetch API data
- ▸async/await - handle asynchronous code
- ▸array.map(), array.filter() - array operations
FAQ
- ▸Is JavaScript a programming language?
- ▸Yes, it is a high-level, interpreted programming language.
- ▸Which browsers support JavaScript?
- ▸All modern browsers fully support JavaScript.
- ▸Can JS run outside the browser?
- ▸Yes, using Node.js or other JS runtimes.
- ▸Is JavaScript the same as Java?
- ▸No, they are different languages with distinct syntax and purposes.
- ▸Do I need HTML/CSS to use JS?
- ▸Not strictly, but they are usually used together for web development.
30-Day Skill Plan
- ▸Week 1: Variables, data types, operators
- ▸Week 2: Functions, loops, conditional statements
- ▸Week 3: DOM manipulation and events
- ▸Week 4: Asynchronous JavaScript (Promises, async/await)
- ▸Week 5: Frameworks, modules, and project structuring
Final Summary
- ▸JavaScript is the core language for dynamic web content and interactivity.
- ▸It runs on both client and server sides.
- ▸Supports multiple paradigms: event-driven, functional, and object-oriented.
- ▸Works with HTML for structure and CSS for styling.
- ▸Essential for modern web development, SPAs, and full-stack applications.
Project Structure
- ▸index.html - main page
- ▸js/main.js - primary JavaScript file
- ▸js/modules/ - reusable modules
- ▸assets/ - images, fonts, media
- ▸package.json - Node.js project configuration
Monetization
- ▸Develop web apps and SaaS products
- ▸Offer freelance front-end or full-stack services
- ▸Create JavaScript libraries or plugins
- ▸Teach JavaScript via courses or workshops
- ▸Contribute to open-source projects for visibility
Productivity Tips
- ▸Use modern frameworks and libraries
- ▸Modularize code for maintainability
- ▸Automate repetitive tasks with scripts
- ▸Use browser dev tools efficiently
- ▸Follow coding best practices
Basic Concepts
- ▸Variables: `var`, `let`, `const`
- ▸Data types: string, number, boolean, object, array, null, undefined
- ▸Functions: declaration, expression, arrow functions
- ▸Control structures: if, for, while, switch
- ▸Events: click, input, load, DOMContentLoaded