What Is JavaScript ?

A high level look at What JavaScript is and how it empowers the web

What Is JavaScript ?

JavaScript is one of the most popular and most widely used programming languages. It can update and change both HTML & CSS, and can calculate, manipulate and evaluate data.

Introduction

JavaScript is a scripting language that allows us to create dynamically updating content, control multimedia, animate images and lots more. It is a dynamically typed language, which means that it performs type checking at runtime. Therefore scripts written in JavaScript can compile even if they contain errors that may prevent the script from running properly.

Like any dynamically-typed programming language, JavaScript puts less constraints on data types when declaring variables. Unlike in static-typing where we're required to specify the data type of your variables before using them, in JavaScript we do not have to, this also means that in JavaScript the same variable can hold different data types. Here is an example of variable declaration in static-typing versus in dynamic typing (JavaScript):

  • Variable declaration in java => static-typing:
    int number = 2;
    
  • Variable declaration in JavaScript => dynamic-typing:
    var number = 2;
    
    Notice how we have to specify which type of number our variable is going to have in Java, and how that's not the case in JavaScript. This means that we can even assign a string value to our variable number later on in our code if we want to without facing any issue. But we won't do the same in java, we'll get an error message, as in Java our variable number has a specific data type.

JavaScript Data Types

In order for JavaScript to operate on a variable, it needs to know something about the type of data contained in that variable.

The different JavaScript Data Types are:

  • Numbers: JavaScript has only one type of numbers, they can be written with or without decimals.
    eg.
    // declaring an integer
    var integer = 5;
    // declaring a decimal
    var decimal = 23.55;
    
  • Strings: are series of characters like in "Me and You". They are written inside quotes, either double or single.
    eg.
    // declaring a string
    var phrase = "JavaScript is awesome";
    
  • Boolean: can only have 2 values true or false. They are basically little on-off switches where true is on and false is off. These two states are mutually exclusive. Note: Boolean values are never written in quotes.
  • Arrays: with arrays we can store several pieces of data in one place. Array are written within square brackets []. We can access the data inside arrays using indexes that specify an entry in the array.
    eg.
    //  declaring an array
    var array = ["one", "two", "three", 1, 2, 3];
    // accessing data in an array
    array[2] // will access the string "three" in the above array as the first index is 0.
    
    Arrays can also contain nested array within them, such arrays are called multi-dimensional arrays. Their data is accessed the same way using indexes.
    eg.
    // declaring a multi-dimensional array
    var multiDimensionArray = [
    ["Robert", 12],
    ["Layla", 19],
    ["Divine", 26],
    ];
    // accessing the number *19* in that multi-dimensional array
    multiDimensionArray[1][1];
    
  • Objects: are similar to arrays except that they are written inside curly braces {}. They also differentiate from arrays in the ways of accessing their data, instead of using indexes to access their data, we access them through what are called properties. Their data is accessed in 2 different manner:
    • Using the dot notation :
      eg.
      // declaring an object
      var myObject = {
      "name": "July",
      "age": 35,
      "nationality": "German"
      };
      // accessing data using the dot notation
      myObject.name; // will access the value of "July"
      
    • Using the bracket notation : The bracket notation is what we use when the name of our property contains spaces.
      eg.
      //declaring an object
      var myObject = {
      "first name": "July",
      "last name": "Mayer",
      "age": 35,
      "nationality": "German"
      };
      // accessing data using the bracket notation
      myObject["last name"]; // accesses the value of "Mayer" 
      myObject["nationality"]; // accesses the value of "German"
      
  • Undefined : in JavaScript, a variable without a value has the default value of undefined, and its type is also undefined.
    eg.
    // Undefined variable
    var noValue;
    
  • etc.

JavaScript Functions:

A function is a reusable block of code designed to perform a particular task. We can invoke a function by using its name followed by (). Every time the function is called, it'll do the same thing we specified it to be doing.
eg.

function myFunction() {
console.log("This is a function");
}
myFunction(); //calling the function

A function can also take various parameters. Parameters are variable that act as placeholders for the values that are to be input in the function when it's called. The actual values that are passed or input in the function when it's called are known as arguments.
eg.

function product(number1, number2) {
number1 * number2;
}
product(2, 5); // Will make a product of 2*5 as they are the arguments passed into the function

Learn More

JavaScript is a huge and amazing language. it would be impossible for me to details all of its amazing features in this article, but if you're interesting in learning more about about it, this MDN documentation about it is just amazing.

You can also learn how to code in JavaScript for free at freecodecamp.

Or you can check my thread about JavaScript on twitter:

Thanks for reading
Aba Nicaisse