JavaScript Syntax and Basics

Ahoy, eager code magicians! Having set up our magical camp, it's time we delve deeper into the mystical language of JavaScript. Today, we'll be exploring the fundamental incantations and magical glyphs (or as the layman calls it, syntax and basics) of JavaScript. Hold on to your wizard hats, this is going to be exciting!

Variables (let, const, var)

In JavaScript, variables are like magical containers that store values for you. There are three keywords we use to declare variables: let, const, and the older var.

let magicNumber = 7;
magicNumber = 8; // The value can be changed
console.log(magicNumber); // Outputs: 8
const magicalCreature = 'unicorn';
// magicalCreature = 'dragon'; // This will result in an error
console.log(magicalCreature); // Outputs: unicorn
var oldMagic = 'witchcraft';
oldMagic = 'alchemy';
console.log(oldMagic); // Outputs: alchemy

Data Types

JavaScript possesses a variety of magical elements, each with its own unique properties. These are the basic data types:

let potionBottles = 7; // Integer
let potionLiters = 1.5; // Floating-point
let spell = "Abracadabra!";
let isWizard = true;
let magic = null;
let magic;
console.log(magic); // Outputs: undefined
let wizard = { name: 'Merlin', age: 150 };
let spells = ['Leviosa', 'Expecto Patronum', 'Expelliarmus'];

Type Conversion and Coercion

Sometimes, you'll need to transform your magical entities from one form to another. That's where type conversion and coercion come in.

let spellCount = '150';
console.log(typeof spellCount); // Outputs: string

spellCount = Number(spellCount);
console.log(typeof spellCount); // Outputs: number
let spellCount = '150';
let totalSpells = 150;

console.log(spellCount == totalSpells); // Outputs: true

Here, even though one is a number and the other is a string, JavaScript coerces them to the same type to make the comparison.

Operators

Operators are the magical glyphs that perform operations on our variables. Here are some of them:

let incantations = 5;
let charms = 2;

console.log(incantations + charms); // Outputs: 7
let magicPower = 5;
let wizardPower = 10;

console.log(magicPower == wizardPower); // Outputs: false
let isWizard = true;
let isWitch = false;

console.log(isWizard && isWitch); // Outputs: false

There you have it, apprentice! You've taken your first step into the labyrinth of JavaScript's syntax and basics. Keep practicing these spells, and soon you'll be enchanting the web with your magic. Until next time, happy coding!

Previous Next

Written © 2024 Written Developer Tutorials and Posts.

𝕏