Technical Blog - JS Fundamentals

javascript wizard

The Unseen Wizard: A Beginner's Guide to JavaScript


JavaScript (JS) is the programming language that makes websites cool. It adds animations, shows interactive elements, and can even remember our preferences when browsing the web . JS is the secret ingredient that brings websites to life.


Imagine you’re building a house:



But how does this wizard cast his spells? Here's a sneak peek into his bag of tricks:


The Document Object Model (DOM)

Think of the DOM as a map of your website. Every element, from text to images to buttons, has a specific place on this map. Web browsers have built-in tools (like Chrome developer tools) that let you see and interact with this map. It’s like a crystal ball that can be used to view what is going on in a website at any time.




Functions

Functions are like little spells the wizard uses to perform specific tasks. Let's say you want the website to display the message "Alacazam" whenever you hover over a button. JS can create a function that takes care of this with this code:



In this example, doMagic is the function name. When you call this function by hovering over the button on the website, it will display the message (and hopefully you don’t turn into a frog!).




Arrays

Arrays are like the wizard's shaft and a place where he can roll out his spells. Spells can only be added or removed from one end which means the wizard needs to be mindful of the order he stores his spells. A practical, at-home example, might be a (rather narrow) cleaning cupboard. This code shows a cleaningCupboard array with each item stored in a specific order.






Objects

Objects are the wizard's well-organised magic chest, with compartments for categorised items. Each compartment is labeled with a key, and the item inside is the value. Sticking with our house example, a dirty bathroom needing to be cleaned could be an object with tasks as key-value pairs:






Control Flow and Loops

These are the wizard's decision-making tools. Imagine having a set number of cleaning chores. Loops allow JS to repeat a set of instructions for a certain number of times, making sure everything gets done, and control flow is what allows JS to make decisions and repeat tasks based on those decisions. This ensures everything gets done, and in the right order. Here's a simple example using an if statement (a decision) and a for loop (repetition):



This code loops through the cleanBathroom array four times and displays a different message based on each task.




This is just a glimpse into the other-worldly powers of JavaScript. It's a special tool that makes websites interactive and user-friendly.


Rub me and see what magic I have instore!