What is JavaScript?

william-monroy

What is JavaScript?

JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. It is the language of the web.

Table of Contents

Basics of JavaScript

JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. It is the language of the web. JavaScript is a programming language that adds interactivity to your website. This happens in games, in the behavior of responses when buttons are pressed or with data entry on forms; with dynamic styling; with animation, etc. This course will cover the fundamental aspects of JavaScript programming starting with how to include JavaScript in your HTML documents, variables, operators, data types, arrays, operators, control statements, objects, strings, and forms. We will also touch on advanced topics such as security, AJAX, and JSON.

JavaScript Basic Syntax

JavaScript is a lightweight, interpreted programming language. It is designed for creating network-centric applications. It is complimentary to and integrated with Java. JavaScript is very easy to implement because it is integrated with HTML. It is open and cross-platform.

First JavaScript Program (Hello World)

// This is a comment
console.log("Hello World!");

Datatypes in JavaScript

JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:

var x; // Now x is undefined
x = 5; // Now x is a Number
x = "John"; // Now x is a String

The latest ECMAScript standard defines seven data types:

Difference between Primitive and Non-Primitive Datatypes

The difference between primitive and non-primitive data types are:


PrimitiveNon-Primitive
Primitive data types are predefined data types.Non-primitive data types are not predefined data types.
Primitive data types are immutable.Non-primitive data types are mutable.
Primitive data types are passed by value.Non-primitive data types are passed by reference.
Primitive data types are stored in the stack.Non-primitive data types are stored in the heap.
Primitive data types are copied by its value.Non-primitive data types are copied by its reference.
Primitive data types are faster than non-primitive data types.Non-primitive data types are slower than primitive data types.

JavaScript Identifiers

All JavaScript variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). The general rules for constructing names for variables (unique identifiers) are:

Operators in JavaScript

JavaScript has the following types of operators. This section describes the operators and contains information about operator precedence.

JavaScript Variables

JavaScript variables are containers for storing data values. In this example, x, y, and z, are variables, declared with the var keyword:

var x = 5;
var y = 6;
var z = x + y;

From the example above, you can expect:

Since ECMAScript 2015, there are two more ways to declare a variable: let and const.

Scope of Variables

The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes.

Wrapper Classes in JavaScript

JavaScript provides three primitive wrapper classes:

These three wrapper classes are immutable (i.e. unchangeable).

JavaScript Comments

Comments can be used to explain JavaScript code, and to make it more readable. Comments can also be used to prevent execution, when testing alternative code. Single line comments start with // Multi-line comments start with /_ and end with _/ (like in C++ and Java)

JavaScript Keywords

JavaScript keywords are used to identify actions to be performed. The var keyword tells the browser to create variables. The function keyword tells the browser to create functions. The return keyword tells the browser to return a value.

JavaScript Data Types

JavaScript variables can hold many data types: numbers, strings, objects and more:

var length = 16; // Number
var lastName = "Johnson"; // String
var x = { firstName: "John", lastName: "Doe" }; // Object

If this content was helpful, please consider getting in touch for more content like this.