JavaScript Basic

Sahidularif
4 min readMay 5, 2021
JavaScript

What is JavaScript?

The simple answer is JavaScript is a scripting or programming language that allows you to implement complex features on web pages. It’s a softly coded multi-function, multithreading programming language.

JavaScript is a programming language like other programming languages (Java, C, C++, C#). You can do all of the arithmetic and function work in JavaScript. It also uses in Server-Side web apps development using Node.JS and Express.JS.

How to use JavaScript on the web page

JavaScript can change HTML content.One of many JavaScript HTML methods is getElementById().The example below “finds” an HTML element (with id=”demo”), and changes the element content (innerHTML) to “Hello JavaScript”:

document.getElementById(“demo”).innerHTML = “Hello JavaScript”;

JavaScript Can Change HTML Styles (CSS)

Changing the style of an HTML element, is a variant of changing an HTML attribute:

document.getElementById(“demo”).style.fontSize = “35px”;

JavaScript Syntax

JavaScript syntax is the set of rules, how JavaScript programs are constructed:

var x, y, z; // Declare Variables
x = 5; y = 6; // Assign Values
z = x + y; // Compute Values

JavaScript console.log()

All modern browsers have a web console for debugging. The console.log() method is used to write messages to these consoles. For example,

let sum = 44;
console.log(sum); // 44

When you run the above code, 44 is printed on the console.

JavaScript Variables

What is a variable? Variable is like a container that contains multiple or single values. So JavaScript variable is a container that contains any type of variable.

Variable declaration: In this example, x, y, and z, are variables, declared with the var keyword:

Example 1:

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

Example 2:

var apple= 5;
var orange= 6;
var total = apple+ orange;//11

Block Binding:

Variable is more formally known as binding. Usually, bindings occur whenever we declare or set a value in a variable. When we declare or initialize a variable, we actually bind a value to a name inside a scope. Scope usually refers to a specific part of a program.So binding occurs whenever we declare and/or initialize a variable using one of these keywords: var, let, and const in JavaScript. For example:

var x

let y

const last = z

Block-Level Declarations

Block-level declarations means the declare variable are not accessible outside the block scope Block scopes are created:

  1. Inside of a function
  2. Inside of a block (indicated by the { and } characters)
function add(){let a = 5;
let b = 7;
return sum = a + b;
}

Block Binding in Loops:

In JavaScript, most often case developers want block level scoping of variables is within for loops. For for this we have to use let not var, cause var is being hoisted. Follow the two examples below:

for (var i=0; i < 10; i++) {
process(items[i]);
}
// i is still accessible here
console.log(i); // 10

Here, outside the loop, i is accessible because we use var

for (let i=0; i < 10; i++) {
process(items[i]);
}
// i is not accessible here - throws an error
console.log(i);

Here, outside the loop, i is not accessible because we use let. So, it will throw an error.

JavaScript Operators

JavaScript have 7 types of operators

  1. Arithmetic operator
  • (+ ) Addition // x + y
  • ( - ) Subtraction // x- y
  • ( * ) Multiplication // x * y
  • ( / ) Division // x / y
  • ( % ) Modulus (Division Remainder)
  • ( ++ ) Increment & // a++ or a=a+1
  • ( — )Decrement // a — or a=a — 1

2. Assignment Operators

Assignment operators assign values to JavaScript variables.

= x = y, +=x += y, -=x -= y , *=x *= y, /=x /= y , %=x %= y, **=x **= y

3. String Operators

The + operator can also be used to add (concatenate) strings.

Example

var text1= ‘What a ’;
var text2= ‘nice day’;
var sentence = text1 + text2 // “What a nice day”

4. JavaScript Type Operators

JavaScript Type operator

5. Bitwise Operators

JavaScript Bitwise Operator

JavaScript Data Types

There are different types of data that we can use in a JavaScript program. For example

const x = 5; // integer
const y = "Hello"; // string

There are eight basic data types in JavaScript. They are:

1. String represents textual data // ‘hello’,

2.Number: refer to an integer or a floating-point number //1, 2.55, 3e-2 etc

3. Boolean Any of two values: true or false // true and false

4. undefined a data type whose variable is not initialized // let a;

5. null denotes a null value // let a = null;

6. Object key-value pairs of collection of data // let student = { };

--

--