JavaScript Basics

lizard made out of circuit board parts on green surface with gold dots above it and below

Note: this page has been created with the use of AI. Please take caution, and note that the content of this page does not necessarily reflect the opinion of Cratecode.

JavaScript is the life of the party when it comes to web development. It's the language that makes websites lively, interactive, and dynamic. Without JavaScript, the internet would be a bland place, like a party without music. So, let's dive into the basics of JavaScript and learn how it adds life to our websites.

Variables

Much like a treasure chest that holds precious gems, variables in JavaScript store valuable data. You can create a variable using the let keyword, followed by its name and the data it should hold.

let favoriteColor = "blue";

In this example, we've created a variable called favoriteColor and stored the string "blue" inside it. You can change the value of a variable at any time.

favoriteColor = "green";

Now, favoriteColor holds the string "green".

Constants

Sometimes, you want to store data that should never change, like the value of pi or the number of days in a week. In JavaScript, you can use the const keyword to create a constant variable.

const daysInWeek = 7;

Here, we've created a constant called daysInWeek and assigned it the value 7. Once defined, constants cannot be changed.

Functions

Functions are the wizards of your JavaScript code, performing magical tasks whenever you call upon them. You can create a function using the function keyword, followed by its name, a pair of parentheses, and a code block enclosed in curly braces.

function greet() { console.log("Hello, world!"); }

In this example, we've created a function called greet which logs "Hello, world!" to the console. To invoke this function, simply call it by its name followed by parentheses.

greet(); // Outputs "Hello, world!"

Conditionals

Imagine JavaScript as a wise oracle, making decisions based on certain conditions. Using if, else if, and else statements, you can create code branches that execute only when specific conditions are met.

let age = 18; if (age >= 18) { console.log("You can vote!"); } else { console.log("You are too young to vote."); }

In this example, JavaScript checks if the age variable is greater than or equal to 18. If it is, it logs "You can vote!" to the console. If not, it logs "You are too young to vote."

Loops

Sometimes, you want to perform a task repeatedly, like playing your favorite song on loop. In JavaScript, you can use for and while loops to cycle through code blocks until a condition is no longer met.

for (let i = 0; i < 5; i++) { console.log("Loop iteration: " + i); }

This for loop runs five times, logging "Loop iteration: 0" through "Loop iteration: 4" to the console.

Now that we've covered the basics of JavaScript, you're ready to start exploring the world of web development and make your websites come alive. Time to hit the dance floor and make some noise with your newfound JavaScript skills!

Hey there! Want to learn more? Cratecode is an online learning platform that lets you forge your own path. Click here to check out a lesson: Making Websites (psst, it's free!).

Similar Articles