What is JavaScript?

A lot of pages suggest learning JavaScript, but what is JavaScript? We get asked about JavaScript a lot at CodeBrainer, that is why we wanted to write a good description of JS so that all of our students would understand what is javaScript and why should they learn it. Also what they can do with it and how does it look. This is a document for all levels of developers but most importantly a useful guide for those who are choosing JavaScript as a first programming language.

JavaScript is a programming language created to be used for web browsers, for making web pages feel more alive. Yes, at first most web pages were static content with the ability to navigate page to page. JavaScript changed all that.

These days JavaScript is part of many software implementations (hosts), including web servers, databases, and for example in some versions of PDF software. 

JavaScript made modern web applications possible. And there is more to come in coming years as boundaries of progress are moved more and more, and most mobile devices support JavaScript with ease.

We use JavaScript for:

  • Interact with user
  • Use functions and calculate useful values for the user to see, use
  • Make changes on UI - Add/Change/Remove elements within DOM
  • Make requests to the servers (REST APIs, AJAX...)
  • Store and read values from cookies
  • Store data locally for faster work with a web page
  • ...

 

 

 

What kind of language is JavaScript?

JavaScript is mostly defined as prototype-based scripting object-oriented language, and it is dynamically typed. Wau, what does that all mean?

 

JavaScript is a prototype-based language

The short explanation is: a class is an instruction on how to make an object, and an object is an instance of that class. A prototype-based language, such as JavaScript, does not make this distinction: it simply has objects. 

We can specify properties for any object; we can do this when we create them or at runtime. This way JavaScript is perfect for communicating with servers, as servers can change data structures and the code will still work (in most cases). 

Any object can become a prototype for another object. This means that the second object will share the first object's properties (this does not mean that values have to be the same).

 

JavaScript is a dynamically typed language

JavaScript is dynamically typed, this means, that variables can change types during runtime. This has advantages and disadvantages. Modern development does not condemn dynamically typed languages, as Test Driven programming has made such progress that there are a lot of advocates of dynamically typed approach. 

 

Is there a way to use types in JavaScript?

There are a few ways to use types “in” JavaScript with using some of the languages that use JavaScript as an intermediate language like (we have added descriptions for languages as they are pretty funny):

  • TypeScript
    • Microsoft describes TypeScript as a superset of JavaScript that compiles to clean JavaScript output.
  • CoffeeScript
    • CoffeeScript is a little language that compiles into JavaScript.
  • Elm
    • Elm, a delightful language for reliable web apps.
  • ..and more

 

What is JavaScript that is known as Vanilla JS? 

Vanilla JavaScript or Vanilla JS is a JavaScript that is not extended by any frameworks or additional libraries. This means that it is standardized and we can use it anywhere. This is great for building libraries and frameworks as they can be used in more situations if they are built from plain JavaScript.

 

JavaScript is great for beginners

JavaScript is a good programming language for beginners for a couple of reasons. JavaScript is all around us, and most of us can understand what it is used for. Also, the great thing about JavaScript is that it can be run online and there are a lot of online editors out there, and this means that it is easy to try. We use plnkr.co for a lot of our examples. 

A great way to begin learning more about JavaScript is to start an easy project. We have a great example at CodeBrainer where you learn about Random numbers in JavaScript; you will change images, see, how data is structured and pick a random choice.

 

Let’s show what is JavaScript with an example

The first example is a simple way to change all the paragraphs on a web page. First, we must create a selector for all the paragraphs. The next step is to change the style for that selector; this means that all the elements that fit in that selector, will be affected by the new style setting. This is also a great example to show what is JavaScript used for since it combines elements on a Web Page and javaScript code.

JavaScript example

Ways to declare variables in JavaScript

  • var
    • var x = 10;
    • This is a normal declaration of a variable. With var, variables have function scope or global scope, depending on where they are defined.
  • let
    • let x = 15;
    • Let keyword declares a variable that is block scoped. We can assign a value at declaration or later.
  • const
    • const x = 23;
    • Const is block scoped like let statement values, but it cannot be changed after declaration.

 

How is an object defined in JavaScript

Objects in JavaScript are very natural, you don’t even have a feeling, that you are working with objects, but in reality you are. Every variable is an object, except for data types. This means that we can make it really great for usage.

JS object

As you can see, we can play with the naming and values right on the spot.  The great thing is that we can access values by key name or with dot notation.

JavaScript object properties 

Arrays in JavaScript

With arrays, we can list more values under a combined name (property, variable,...). With that, we get a few new abilities like iterating through all the values, calculations on all the elements...

JavaScript array

This is an example of how a simple array for ingredients would look like.

 

Adding methods to object in JavaScript

One of the great things is that we can also add methods to the objects if we need to. Let’s use our example from above and make it a little bit more advanced. Let’s add ingredients. We will add a property named ingredients that will be an empty array at the start. But we will add a method for adding ingredients, named addIngredient.

JavaScript object methods

Last lines show how we can add new values to the ingredients array.

 

 

 

Data types in JavaScript 

JavaScript has six data types that are primitives:

  • Boolean
  • Number
  • String
  • Null
  • Undefined
  • Symbol (new in ECMAScript 6)

And of course

  • Object

Here is an example of primitive data types:

JavaScript data types  

Loops in JavaScript with examples

Let’s show all the loops in JavaScript with examples:

  • for statement
     JavaScript loop for
    • Here we have a loop that starts with a value and iterates while a condition is true; at the end, we specify how we want to increase the value of a variable.
  • do...while statement
    JavaScript loop do while
    • With do while loop we need to be careful that at the beginning values meet our criteria. If the list of ingredients were empty, we would get an error here.
  • while statement
    JavaScript loop while
    • While loop will check conditions from the beginning.
  • for...in statement
    JavaScript loop for in
    • For array, this is a better option than building it ourselves, in the end, we get an index of objects in our array, and the code is much cleaner.
  • for...of statement
    JavaScript loop for of
    • This is the most modern version of a loop since it will return objects it iterates through, this way most of the work is already done for us and the code looks clean.

 

What is JavaScript used for?

One of the first goals of JavaScript was to be the glue language for web pages; this means that it would be there in the markup of the web page. This is still possible today even though most of JavaScript code has moved to js files, but you can use a script tag and put all of your code inside of this tag.

Without JavaScript web pages would be a lot more boring. You probably know some pages that slide more information on the page as you scroll down. This is done with JavaScript. Most of us buy things online; this is done with JavaScript as well; JavaScript takes care of checking values you enter and communicating with a server

These days it is not hard to find online games to play. Most online games these days are implemented using JavaScript as their programming language.

What is JavaScript best used for you would ask? We think that JavaScript is best used in combination with HTML and CSS. This means that knowing all of them will make you a web developer and we have made a great list of web developer skills you should learn before you can call yourself a web developer.

 

Is Java similar to JavaScript?

Although there are a few similarities between JavaScript and Java, most similarities end with the language name :D 

It is a fact that Netscape wanted to create a language that would complement Java and that their syntax should be similar, but modern JavaScript and Java are not that alike, and they differ greatly in design.

 

Is JavaScript open source? Who owns JavaScript?

We are in a kind of in a mixed situation here. Most JavaScript engines are based on ECMAScript which was started by Netscape. In November 1996, Netscape submitted JavaScript to ECMA International to carve out a standard specification, which other browser vendors could then implement based on the work done at Netscape. Otherwise, "JavaScript" is a trademark of the Oracle Corporation in the United States.

 

 

 

JavaScript is here to stay

You may ask what the main advantage of JavaScript is? JavaScript is one of the most used programming languages out there. And there is a lot of explanations for that. JavaScript is still a universal programming language for programming web pages; it can be used almost anywhere. More than 90% of browsers will be able to work with JavaScript.

It is also vital that JavaScript is growing strong on the server-side as well. This means that there is one more plus side to learning and building things with JavaScript.

This is why, even with all the concerns we have about JavaScript, we think that JavaScript is here to stay and learning how to program in JavaScript is a great choice.