Abhishek Luv

Online Trainer for ASP.NET MVC 5.2.3, Angular, ASP.NET Core, Entity Framework & EF Core

JavaScript Beginner's Series : Part 1

Welcome to the JavaScript Beginner’s Series.

Topics we’ll be covering

Part 1

  • JavaScript Overview
  • Ways to add JavaScript code on a HTML page
  • Variables, DataTypes, Operators and Expressions

Part 2 (Coming Soon)

  • Conditional Statements : if, else, if else if, switch
  • Looping Statements : while, do..while, for, for..in

Part 3 (Coming Soon)

  • Functions
  • Arrays
  • Understanding the Scope : Availability of variables globally or locally

Part 4 (Coming Soon)

  • Understanding Objects Literals
  • Objects in JavaScripts : Number, Boolean, String, Date, Math
  • Understanding the DOM : Document Object Model

JavaScript Beginner’s Series : Part 1

JavaScript Overview

JavaScript is the assembly language of the Web. JavaScript is a high-level, dynamic, untyped, and interpreted programming language. Do not confuse JavaScript with Java.

In this beginner’s series, we’ll be learning the basic building blocks of JavaScript that every beginner or a newbie needs to know.

Ways to add JavaScript code on a HTML page

Adding JavaScript Code using the <script></script> tag with the type attribute

Adding JavaScript file using the src attribute of the <script></script> tag in the <head></head> section

JavaScript code or file is placed in the <head> section when you want to run some code on a click event somewhere on your web page.

Adding JavaScript code or file using the <script></script> tag in the <body></body> section

JavaScript code or file is placed in the <body> section when you want to generate some content on the web page using some JavaScript code as soon as the page loads.

Variables, DataTypes, Operators and Expressions

Variables

var keyword is used to declare a variable in JavaScript. You should always use the var keyword to declare a variable in JavaScript.

Types of variables in JavaScript :

  • Global variables (Global Scope)
  • Local and Block level variables (Local Scope)

Variables in JavaScript are written in CamelCase for ex: isItGlobal.

Let’s look at some code.

Data Types

  • Numbers : eg: 157, 457
  • Strings : eg: “This is string”, ‘This is string’
  • Booleans : eg: true or false
  • Undefined

In JavaScript, datatype is inferred by the type of data or value assigned for a variable.

For ex: If we assign 124 value to a variable var myValue = 124. The data type for myValue will be Number.

Let’s look at some code and find out the data type of a variable (using the typeof keyword).

Operators

Operators allows us to compute data in a programming language.

JavaScript support the following types of operators :

  • Arithmetic
  • Comparison
  • Logical
  • Assignment
  • Ternary

In order for an operator to work it needs two operands.

For ex:

4 + 5 = 9 Here, 4 and 5 are the two operands and + is the operator.

Arithmetic Operator

Comparison Operator

Logical Operator

Assignment Operator

The Assignment assigns values from the right side operand to the left side operand.

+= (Add & Assignment) : adds the right operand to the left operand and assigns the result to the left operand.

-= (Subtract & Assignment) : subtract the right operand to the left operand and assigns the result to the left operand.

*= (Multiple & Assignment) : Multiples the right operand to the left operand and assigns the result to the left operand.

/= (Division & Assignment) : Divides the right operand to the left operand and assigns the result to the left operand.

%= (Modulus & Assignment) : Modulus operation happens from right operand to left operand and assigns the result to the left operand.

Ternary Operator

The Ternary operator first checks the expressions for a true or false value and then executes the given statements.

For ex:

Part 2 coming soon

Thank you for reading.

Stay tuned!

Take care

Abhishek Luv

Comments