JavaScript 101: Variables and Data Types

javascript-logo

This article is part of the JavaScript 101 series, you can find the previous part on the following link: JavaScript 101: Into Programming.

JavaScript often called JS, is a high-level, interpreted scripting language that conforms to the ECMAScript specification. JavaScript is a dynamic typed, prototype-based object-orientated language with functions as first-class citizens.

JavaScript is one of the core technologies of the Web, it enables interactive web pages and is an essential part of web applications. The majority of websites use it, and major web browsers have a dedicated JavaScript engine.

As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) programming styles.

Initially implemented only on client-side in web browsers, JavaScript engines are now embedded in many other types of software, including server-side in web servers and databases, and in non-web such as word processors and PDF software, and in runtime environments that make JavaScript available for writing mobile and desktop applications, including desktop widgets.

Variables

Variables are containers for storing data values. JavaScript uses reserved keyword var ( let and const were also added in ES6 ) to declare a variable. A variable must have a unique name.. Storing a value in a variable is called variable initialization using equal to (=) operator. You can do variable initialization at the time of variable creation or at a later point in time when you need that variable.

In the example below, we are declaring and initializing two variables named x and y with values 5 and 10.

var x = 5;  
var y = 10;

JavaScript is an untyped language. This means that a JavaScript variable can hold a value of any data type.

There are two types of variables in JavaScript: local variable and global variable.

  • Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code.
  • Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

Array

In JavaScript, array is a variable that is used to store different elements. It is often used to store a list of elements and access them by a single variable. There are two ways to declare an array, but most of the time's option 1 is used.

var list = []; // option 1 
var list = new array(); // option 2

Initialization of an Array:

var list = [1, 2, 3]; // option 1 
var list = new array(1, 2, 3); // option 2

In the code snippet above we have an array with a name of "list" with three elements 1 at index 0, 2 at index 1, 3 at index 2. Arrays start from index 0. Let's see how we can access an element from an array:

var list = [1, 2, 3];
console.log(list[0])

And we got the first element with a value of 1. Like in the example you can access every element by its index, using [] and the index of the element inside.

Data Types

There are six primitive data types and the last one is object data type in JavaScript.

  1. Number represents both integer and floating-point numbers.
  2. Boolean represents a logical entity and can have only two values: true and false.
  3. String type is used to represent textual data. It can be used as ' ' or " ".
  4. Undefined has a value of undefined when value is not assigned.
  5. Null can have just one value: null. It’s just a special value that represents nothing.
  6. Symbol is a unique and immutable value and may be used as the key of an Object property.
  7. Object in computer science is an object with a value in memory which is possibly referenced by an identifier.

Next part: JavaScript 101: Conditionals and Loops.




#javascript #javascript101

Author: Aleksandar Vasilevski |

Loading...