DEV Community

Cover image for JavaScript
Vairavan
Vairavan

Posted on

JavaScript

JavaScript is one of the most popular programming languages in web development. It is mainly used to make websites interactive and dynamic. With JavaScript, we can create things like buttons, animations, calculators, login forms, games, and much more.

Variables

Variables used to store data in JavaScript.

JavaScript has 3 ways to create variables:

  1. var
  2. let
  3. const

var

var has global scope and it is the old way of declaring variables.

var a = 10;
console.log(a);
output:
10
Enter fullscreen mode Exit fullscreen mode

let

let has block scope. It can only be accessed inside { }.

Example:

let a = 10;

{
  let a = 20;
  console.log(a); // 20
}

console.log(a); // 10

OutPut :
10
Enter fullscreen mode Exit fullscreen mode

const

const is used for constant values. Its value cannot be reassigned.

const c = 30;
console.log(c);

OutPut:
30                 āœ…

Enter fullscreen mode Exit fullscreen mode
const weekdays = 7;

weekdays = 10;     āŒ Error

OutPut:

TypeError: Assignment to constant variable
Enter fullscreen mode Exit fullscreen mode

But if the value is an array or object, we can change values inside it.šŸ‘

Data Types

JavaScript has different data types.

1.Primitive Data Types

These store single values.

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

Example:

  1. let age = 20;
  2. let name = "Surya";
  3. let Student = true;
  4. let salary =null;
  5. let a

2.Non-Primitive Data Types

These can store multiple values.

  1. Array
  2. Object

Operators

Operators are symbols that help us perform calculations, comparisons, and logical operations in JavaScript.

1. Arithmetic Operators

Operator Meaning

  • { + } Add
  • { - } Subtract
  • { * } Multiply
  • { / } Divide
  • { % } Remainder

Example:

let a = 10;
let b = 5;

console.log(a + b);
output:
15
console.log(a - b);
output:
5
Enter fullscreen mode Exit fullscreen mode

2.Comparison Operators

*comparison of Two values *

Operator Meaning

  • { == } Equal
  • { === } Strict Equal
  • { != } Not Equal
  • { > } Greater Than
  • { < } Less Than

Example:

let a = 10;
let b = 5;

console.log(a > b);

Output:
true
Enter fullscreen mode Exit fullscreen mode

3.Logical Operators

Logical operators are used to combine two or more conditions.

Operator Meaning

  • { && } AND → both conditions must be true
  • { || } OR → any one condition can be true
  • { ! } NOT → changes true to false and false to true

Example:

Use &&

let age = 20;

console.log(age > 18 && age < 30);

Output:

true

let age = 20;

console.log(age < 18 && age < 30);

Output:

false
Enter fullscreen mode Exit fullscreen mode

Use ||

let age = 20;

console.log(age > 18 || age < 30);

Output:

true

let age = 20;

console.log(age < 18 || age < 30);

Output:

true

Enter fullscreen mode Exit fullscreen mode

Increment Operator ++

Used to increase Value.

Two Type of increment operators

  • ++a--- Pre-increment
  • a++--- Post-increment

Example:

let a = 5;

a++;

console.log(a);

Output:

6

let a = 5;
let b = a;

a++;


console.log(a);
console.log(b);

Output:

6
5

Enter fullscreen mode Exit fullscreen mode

Decrement Operator --

Used to decrease value

Two Type of Decrement operators

  • --a ---Pre-decrement
  • a-- ---Post-decrement

Example:

let a = 5;

a--;

console.log(a);

Output:

4
Enter fullscreen mode Exit fullscreen mode

Arrays

Array is used to Store Multiple Values in a Single Variable [].

Example:

const marks = [79, 70, 93, 100];

 index:       0   1   2    3

console.log(marks[1]); 

 Output:
 70
Enter fullscreen mode Exit fullscreen mode

Multiple Data Types in One Array

Arrays can store different data types together.

const values = [10, true, "abc", 50];
Enter fullscreen mode Exit fullscreen mode

Changing Array Values with const

Call the index Num when Change the Value.

When using const, you cannot reassign the whole array.

But you can change values inside the array.

const marks = [10, 20, 0, 11, 5];

marks[2] = 99;

console.log(marks);
Enter fullscreen mode Exit fullscreen mode

Functions

A function is a reusable block of code.

It runs only when the function is called.

Function Declaration

function add(no1, no2) {

}

add(5, 10);
Enter fullscreen mode Exit fullscreen mode

Arrow Function

Arrow function is a shorter way to write functions.

const add = (no1, no2) => no1 + no2;

add(10, 15);
Enter fullscreen mode Exit fullscreen mode

Return Value

The return keyword sends a value back from the function.

function add(no1, no2) {
  return no1 + no2;
}

let result = add(5, 7);

console.log(result);

OutPut:

 12
Enter fullscreen mode Exit fullscreen mode

Hoisting

Hoisting means JavaScript moves declarations to the top before running the code.

-Functions are hoisted āœ…
-let and const variables are not hoisted āŒ

let result = add(5, 7);

console.log(result);

function add(no1, no2) {
  return no1 + no2;
}
Enter fullscreen mode Exit fullscreen mode

Objects

An object stores data in key : value pairs.

  const laptop = {
  brand: "HP",
  price: 35000,
  model: "HP05"
};

console.log(laptop.brand);
console.log(laptop.price);
Enter fullscreen mode Exit fullscreen mode

Objects with Functions

Objects can also contain functions.

const obj = {

  type: "object",

  state: "active",

  behavior: function() {
    console.log("Object is working");
  }

};

obj.behavior();
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy šŸŽ–ļø

You've missed two primitive types: Symbol and BigInt