Javascript Basic Concepts 💫

What Is JavaScript Used For?

Of course, JavaScript wouldn’t be a cornerstone of the programming world if it was just about pressing buttons. The practical applications of JavaScript are almost endless, but we’ve highlighted some of the most common uses down below.


⚫️ JavaScript Output

JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  1. Writing into an HTML element, using innerHTML.
  2. Writing into the HTML output using document.write().
  3. Writing into an alert box, using window.alert().
  4. Writing into the browser console, using console.log().

You should know!

Using document.write() after an HTML document is loaded, will delete all existing HTML

JavaScript Statements

let x, y, z; // Statement 1 x = 5; // Statement 2 y = 6; // Statement 3 z = x + y; // Statement 4

A good practice is to put spaces around operators ( = + - * / ):

document.getElementById("demo").innerHTML = "Hello Imran";

JavaScript Programs

  1. A computer program is a list of "instructions" to be "executed" by a computer.
  2. In a programming language, these programming instructions are called statements.
  3. A JavaScript program is a list of programming statements.

JavaScript Syntax

// How to create variables: var x; let y; // How to use variables: x = 5; y = 6; let z = x + y;

JavaScript Values

The JavaScript syntax defines two types of values:

  1. Fixed values
  2. Variable values

Fixed values are called Literals.

Variable values are called Variables.


JavaScript Literals

The two most important syntax rules for fixed values are:

  1. Numbers are written with or without decimals:
10.50 1001
  1. Strings are text, written within double or single quotes:
"Imran" 'imran'

JavaScript Variables

In a programming language, variables are used to store data values. JavaScript uses the keywords var, let and const to declare variables.

let x; x = 6;

  1. In these examples, using var or let will produce the same result.
  2. You will learn more about var,let and const later in comming tutorial.

JavaScript Operators

JavaScript uses arithmetic operators ( + - * / ) to compute values:

(5 + 6) * 10

JavaScript uses an assignment operator ( = ) to assign values to variables:

let x, y; x = 5; y = 6;

JavaScript Comments

Not all JavaScript statements are "executed".

Code after double slashes // or between /* and */ is treated as a comment.

let x = 5; // I will be executed // x = 6; I will NOT be executed

JavaScript Data Types

JavaScript has 8 Datatypes

  1. String
  2. Number
  3. Bigint
  4. Boolean
  5. Undefined
  6. Null
  7. Symbol
  8. Object

The Object Datatype

The object data type can contain 👇🏾

  1. An object
  2. An array
  3. A date
// Numbers: let length = 16; let weight = 7.5; // Strings: let color = "Yellow"; let lastName = "Johnson"; // Booleans let x = true; let y = false; // Object: const person = {firstName:"John", lastName:"Doe"}; // Array object: const cars = ["Saab", "Volvo", "BMW"]; // Date object: const date = new Date("2022-03-25");