DEV Community

Cover image for JavaScript Fundamentals: Data Types, Compiler vs Interpreter & Variables
Kavitha
Kavitha

Posted on • Edited on

JavaScript Fundamentals: Data Types, Compiler vs Interpreter & Variables

1. JavaScript Data Types
JavaScript is a dynamically typed language, which means you don’t need to specify the type of a variable explicitly.

Types of Data Types
Primitive Data Types
These store single values:

Number → let a = 10;
String→ let name = "John";
Boolean → let isActive = true;
Undefined → let x;
Null → let y = null;
BigInt → let big = 12345678901234567890n;
Symbol → Unique identifiers

Non-Primitive Data Types
These store collections of values:

Object

let person = {name: "John", age: 20};

Enter fullscreen mode Exit fullscreen mode

Array

let numbers = [1, 2, 3, 4];
Enter fullscreen mode Exit fullscreen mode

Function

function greet() {
  console.log("Hello");
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:
Understanding data types helps you write better and error-free JavaScript code.

2. Compiler vs Interpreter
Programming languages use translators to convert code into machine language.

Compiler

  • Translates entire code at once
  • Faster execution after compilation
  • Errors shown after full compilation

Example: C, C++

Interpreter

  • Translates line by line
  • Slower but easier to debug
  • Stops immediately when error occurs

Example: JavaScript, Python

JavaScript Special Case

  • JavaScript uses a Just-In-Time (JIT) Compiler, which is a mix of both compiler and interpreter.
  • Engines like V8 JavaScript Engine convert JS into machine code efficiently.

Conclusion:

  • JavaScript is interpreted but optimized using modern compilation techniques.

3. Variables in JavaScript

  • Variables are used to store data values.

Ways to Declare Variables
var

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

let

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

const

  • Block scoped
  • Cannot be changed
const PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

Best Practice:
Use let and const instead of var in modern JavaScript.

Example Programs
1.

let a = 10;
let b = a;

b = 20;

console.log(a); 
console.log(b);
Enter fullscreen mode Exit fullscreen mode

Output:

10
20
Enter fullscreen mode Exit fullscreen mode

2.

let obj1 = {name: "John"};
let obj2 = obj1;

obj2.name = "David";

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

Output:

David
Enter fullscreen mode Exit fullscreen mode

Top comments (0)