Java script Variables Its Types With Full Example

  • JavaScript is a dynamically-typed language, which means that variable types are not explicitly declared, and they can change during the execution of a program. However, JavaScript has different data types that variables can hold. Here are some examples:

Numbers

javascript
  let age = 25;
  let temperature = 98.6;

Strings

javascript
  let name = "John";
  let message = 'Hello, World!'; 

Booleans

javascript
  let isStudent = true;
  let hasJob = false;

Arrays

javascript
  let numbers = [1, 2, 3, 4, 5];
  let fruits = ['apple', 'orange', 'banana'];

Objects

javascript
 let person = {
     firstName: 'John',
     lastName: 'Doe',
     age: 30,
     isStudent: false
 };

Undefined

javascript
 let undefinedVariable;

Null

javascript
 let nullVariable = null;

Functions

javascript
  function greet(name) {
      return 'Hello, ' + name + '!';
  }

  let greeting = greet('Alice');

Remember that JavaScript is loosely typed, so a variable’s type can change during the program’s execution:

javascript
let x = 10; // x is a number
x = 'Hello'; // now x is a string
x = true; // now x is a boolean
  • This flexibility allows for dynamic and expressive coding but requires careful attention to avoid unexpected behavior. If you’re using modern JavaScript (ES6 and later), you can also use const and let to declare variables. const is used for constants (values that shouldn’t be changed), and let is used for variables whose values can be reassigned.