In this tutorial, we will explore what JavaScript is, its uses, and provide some basic examples to help you get started with this powerful programming language.
// Example 1: Basic JavaScript Syntax
console.log("Hello, World!");
// Example 2: Variables and Data Types
let name = "Alice"; // String
let age = 25; // Number
let isStudent = true; // Boolean
console.log(`Name: ${name}, Age: ${age}, Is Student: ${isStudent}`);
// Example 3: Function Declaration
function greet(userName) {
return `Hello, ${userName}!`;
}
console.log(greet(name));
// Example 4: Array and Loop
let fruits = ["Apple", "Banana", "Cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
JavaScript is a versatile programming language primarily used for web development. It allows developers to create interactive and dynamic web pages. Here’s a breakdown of the code examples provided:
- Basic JavaScript Syntax:
- The first line uses
console.log()
to print “Hello, World!” to the console. This is often the first program written when learning a new language.
- The first line uses
- Variables and Data Types:
- We declare three variables:
name
,age
, andisStudent
. let
is used to declare variables, and we assign different data types: a string forname
, a number forage
, and a boolean forisStudent
.- The template literal syntax (using backticks) allows us to easily embed variables into strings.
- We declare three variables:
- Function Declaration:
- The
greet
function takes a parameteruserName
and returns a greeting message. - We call this function with the
name
variable and log the result.
- The
- Array and Loop:
- We create an array called
fruits
containing three fruit names. - A
for
loop iterates through the array, logging each fruit to the console.
- We create an array called
JavaScript is not just limited to web pages; it can also be used for server-side development, mobile app development, and even game development. Its flexibility and ease of use make it a popular choice among developers. So, whether you’re looking to enhance your website or dive into full-stack development, JavaScript is a fantastic language to learn!
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com