JavaScript Tutorial Basics for Beginners

April 1, 2023

1. Introduction to JavaScript

JavaScript is a programming language used to create dynamic and interactive websites. JavaScript code can manipulate HTML and CSS content on a web page to create animations, perform calculations, and add interactivity to forms and buttons.


<!DOCTYPE html>
        <title>My First JavaScript</title>
            h1 {
                color: blue;
            function changeColor() {
                document.getElementById('heading').style.color = 'red';
        <h1 id="heading" onclick="changeColor()">Welcome to my website</h1>

2. JavaScript Variables and Data Types

Variables are used to store data in JavaScript. In JavaScript, variables can hold different kinds of data types such as numbers, strings, booleans, null, undefined, objects, and arrays.


// create a variable 
var myName = 'Sarah';

// log the variable to the console

// create an array variable
var colors = ['red', 'green', 'blue'];

// log an array element to the console

3. JavaScript Operators and Control Structures

Operators are used to perform operations on JavaScript variables. Some of the common operators used include arithmetic, comparison, and logical operators. Control structures such as if-else statements, for-loops, and switch statements are used to control the execution of JavaScript code.


// create variables
var x = 5;
var y = 10;

// add the variables together
var sum = x + y;

// compare the variables
if (x > y) {
    console.log('x is greater than y');
} else {
    console.log('y is greater than x');

// use a for loop to iterate through an array
var colors = ['red', 'green', 'blue'];
for (var i = 0; i < colors.length; i++) {

4. JavaScript Functions

Functions are used to group JavaScript code to perform a specific task. Function parameters are used to pass data to the function, and the return statement can be used to return data from the function.


// create a function to calculate the area of a rectangle
function calculateArea(width, height) {
    var area = width * height;
    return area;

// call the function and log the result
var rectangleArea = calculateArea(10, 20);

5. JavaScript Objects

JavaScript objects are used to group related data and code. Objects are comprised of properties and methods. Properties are variables within the object, while methods are functions within the object.


// create an object with properties and methods
var myObject = {
    name: 'Sarah',
    age: 30,
    hobbies: ['reading', 'photography', 'yoga'],
    sayHello: function() {
        console.log('Hello, my name is ' +;

// access object properties and call methods


This basic JavaScript tutorial covered some essential elements of JavaScript that will help you get started. As your knowledge of JavaScript increases, you will learn more about advanced techniques such as AJAX, APIs, and advanced JavaScript frameworks.