Understanding the Basics of JavaScript

Before we start learning basics of JavaScript let’s look on few essential questions which needed to be addressed before going forward.

Q.1 Who is in charge of JavaScript?

ECMA International, it is the organization that creates many different standards for technology.

Q.2 What is ECMASCRIPT?

ECMAScript itself is not a programming language, but a programming language standard (a language specification standardized as a language) whose specification is implementable as a programming language (notably JavaScript), or implementable in servers, browsers or embedded applications and services. In the ECMAScript® 2022 Language Specification introduction by the ECMA International Technical Committee 39.

Q.3 What is JavaScript?

JavaScript often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.

Q.4 How do I get JavaScript?

You don’t have to get or download JavaScript.

JavaScript is already running in your browser on your computer, on your tablet, and on your smart phone. JavaScript is free to use for everyone.

Getting Started with JavaScript

Primitive Types of JavaScript Variable

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined

1. Number

Unlike manyother Programming language, JavaScript doesn’t define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

Integers are accurate up to 15 digits in JavaScript whereas maximum number of decimals is 17.

We can do all the arithmetic operation like Add,Subtract, Multiply and Divide using JavaScript number.

Examples are given below: –

Javascript number

NaN – Not a Number

NaN is JavaScript reserved word indicating that a number is not a legal number.

NaN Console

2. String

A JavaScript string is zero or more characters written inside quotes. We can use either single quotes or double quotes for writing string in variable.

String

You can use quotes inside a string, if they don’t match the quotes surrounding the string:

Double quotes

3. Boolean

A JavaScript Boolean represents one of two values: true or false.

Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, JavaScript has a Boolean data type. It can only take the values true or false.

Console log
Boolean

4. Null

Null means an empty or non-existent value. Null is assigned, and explicitly means nothing.

null
null hit control

5. Undefined

Undefined means a variable has been declared, but the value of that variable has not yet been defined. For example:

Undefined

Let and Const Keyword

Let keyword is used to definea variable whose value can be reassigned or redeclared but on the other hand variable which is declared with keyword const cannot be redeclared.

Variables defined with const behave like let variables, except they cannot be reassigned.

Const Keywords
hit control
assignment to constant variable

JavaScript Arithmetic:

JavaScript Arithmetic

Example: –

Example of JavaScript Arithmetic
Increment

JavaScript Assignment Operators

JavaScript Assignment Operator

JavaScript Function

Function are one of the fundamental building blocks in JavaScript. A JavaScript function is a block of code designed to perform a task. A JavaScript function is defined with the function keyword, followed by a name, followed by parenthesis ().

Function name can contain letters,digits,underscore and dollar signs.

In parenthesis we can pass parameters names separated by commas:

Function name

Function parameters are listed inside the parentheses () in the function definition.Function arguments are the values received by the function when it is invoked.Inside the function, the arguments (the parameters) behave as local variables.

When JavaScript reaches a return statement, the function will stop executing.If the function was invoked from a statement, JavaScript will “return” to execute the code after the invoking statement.Functions often compute a return value. The return value is “returned” back to the “caller”:

Function returns the product

JavaScript Object

An object in real world can be defined as any entity which have some visible or inherent properties like Color,Weight, Shape etc.

Similarly, in JavaScript Object can be defined by defining a variable and initializing it with some predefined properties and methods that an object must contain. For example: –

JavaScript Object

Object are variable too, but Object can contain multiple properties and values. Here in the given example Car is an object with properties like Type, Model and Color.  We can have multiple objects like car1, car2 with different types, models and color.

Accessing Object Properties:

You can access object properties in Two ways.

  1. ObjectName.ProperyName
  2. ObjectName[“PropertyName”]

For Example

Accessing Object Properties

JavaScript Array

JavaScript Array is used to store multiple value in a single Variable. For Example: –

Let  Phone =  [“iPhone” , “Samsung” , “Vivo” , “Nokia” ]

We can access element of array by referring to the index of it. Index starts from 0 to N-1.

JavaScript Array

JavaScript Switch Statement

Use the switch statement to select one of many code blocks to be executed.

Syntax of Switch Statement

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, the default code block is executed.

For Example: –

Syntax of Switch Statement
No value found

JavaScript Loops

Loops can execute Block of Code several Times. Loops can be used if you want to run the same code over and over again with different values by defining some entry or exit condition.

Different Kinds of Loop in JavaScript

  • for – loops through a block of code a number of times
  • for/in – loops through the properties of an object
  • for/of – loops through the values of an iterable object
  • while – loops through a block of code while a specified condition is true
  • do/while – also loops through a block of code while a specified condition is true

for Loop

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example: –

for (i = 0; i < 5; i++) {
  text += “The number is ” + i + “
“;
}

The for/In Loop

The JavaScript for/in statement loops through the properties of an Object:

Syntax of for/In Loop

for (key in object) {
  // code block to be executed
}

Example: –

var person = {fname:”John”, lname:”Doe”, age:25};

var text = “”;
var x;
for (x in person) {
  text += person[x];
}

  • The for in loop iterates over a person object
  • Each iteration returns a key (x)
  • The key is used to access the value of the key
  • The value of the key is person[x]

The JavaScript for/in statement can also loop over the properties of an Array:

for (variable in array) {
  code
}

Variable in array

The For/Of Loop

The JavaScript for/of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:

Syntax

for (variable of iterable) {
  // code block to be executed
}

variable – For every iteration the value of the next property is assigned to the variable. Variable can be declared with const, let, or var.

iterable – An object that has iterable properties.

Examples: –

Iterable An object that has iterable properties

The While Loop

The while loop loops through a block of code as long as a specified condition is true.

Syntax: –

while (condition) {
  // code block to be executed
}

Example: –

while (i < 10) {
  text += “The number is ” + i;
  i++;
}

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax: –

do {
  // code block to be executed
}
while (condition);

Example: –

do {
  text += “The number is ” + i;
  i++;
}
while (i < 10);

Leave a Reply

Your email address will not be published. Required fields are marked *