DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on

JavaScript Basics

JavaScript is one of the most important programming languages used in web development.
It helps make websites interactive and dynamic.

we will understand:

  • What is JavaScript
  • Data Types in JavaScript
  • Difference between Primitive and Non-Primitive
  • Variables in JavaScript

1. What is JavaScript?

JavaScript is a versatile, dynamically typed programming language that brings life to web pages by making them interactive. It is used for building interactive web applications, supports both client-side and server-side development, and integrates seamlessly with HTML, CSS, and a rich standard library.

  • JavaScript is a single-threaded language that executes one task at a time.

  • It is an interpreted language which means it executes the code line by line.

  • The data type of the variable is decided at run-time in JavaScript, which is why it is called dynamically typed.

Example

<html>
<head></head>
<body>
    <h1>Check the console for the message!</h1>
    <script>
        console.log("Hello, World!");
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World

2. Data Types in JavaScript

What is a Data Type?

A data type defines what kind of value a variable holds (number, text, etc.).

JavaScript is dynamically typed, meaning you don’t need to define the type manually.

Types of Data in JavaScript

According to W3Schools, JavaScript has 8 data types:

Primitive Data Types (7 types)

1. Number

let x = 10;
Enter fullscreen mode Exit fullscreen mode

2. String

let name = "Athithya";
Enter fullscreen mode Exit fullscreen mode

3. Boolean

let isStudent = true;
Enter fullscreen mode Exit fullscreen mode

4. Undefined

let a;
Enter fullscreen mode Exit fullscreen mode

5. Null

let value = null;
Enter fullscreen mode Exit fullscreen mode

6. BigInt

let big = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

7. Symbol

let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

These are called primitive because they store a single value.

Non-Primitive Data Type

Object

let person = {
  name: "Athithya",
  age: 21
};
Enter fullscreen mode Exit fullscreen mode

Objects can store multiple values (key-value pairs).

Difference Between Primitive and Non-Primitive

Feature Primitive Non-Primitive
Type Basic data Complex data
Storage Single value Multiple values
Mutability Immutable Mutable
Examples Number, String Object, Array
Memory Stored directly Stored by reference

Example

// Primitive
let a = 10;
let b = a;
b = 20;

console.log(a); // 10

// Non-Primitive
let obj1 = {name: "A"};
let obj2 = obj1;
obj2.name = "B";

console.log(obj1.name); // B
Enter fullscreen mode Exit fullscreen mode

Primitive β†’ value is copied
Non-Primitive β†’ reference is copied

3. Variables in JavaScript

What is a Variable?

A variable is a container used to store data values.

Ways to Declare Variables

1. var

var x = 10;
Enter fullscreen mode Exit fullscreen mode
  • Old method
  • Function scoped

2. let

let y = 20;
Enter fullscreen mode Exit fullscreen mode
  • Block scoped
  • Can be updated

3. const

const z = 30;
Enter fullscreen mode Exit fullscreen mode
  • Cannot be reassigned
  • Must be initialized

Example

let name = "Athithya";
let age = 21;
const country = "India";

console.log(name, age, country);
Enter fullscreen mode Exit fullscreen mode

Output:

Athithya 21 India

typeof Operator

You can check data type using:

let x = "Hello";
console.log(typeof x);
Enter fullscreen mode Exit fullscreen mode

Output:

string

The typeof operator returns the type of a variable.

Reference

Top comments (0)