Loading...
Loading...
00:00:00

Variable and Data Types

How to Create variables

In JavaScript, variables are used to store values that can be later accessed and modified. There are three ways to declare variables in JavaScript:

  1. var: var is the oldest way of declaring variables in JavaScript. It has a function scope, which means that a variable declared using var is accessible within the function where it was declared (and any nested functions), but not outside of that function. var variables can be re-declared and re-assigned, which can sometimes lead to unexpected behavior.

  2. let: let is a newer way of declaring variables in JavaScript that was introduced in ES6. It has a block scope, which means that a variable declared using let is only accessible within the block of code where it was declared. Unlike var, a let variable cannot be re-declared, although it can be re-assigned.

  3. const: const is another way of declaring variables in JavaScript that was introduced in ES6. Like let, const has a block scope and cannot be re-declared. However, const variables cannot be re-assigned once they have been initialized, which makes them useful for declaring values that should not change.

Here are some examples of declaring variables using each of these keywords:

var myVariable = "Hello"; // declaring a variable using var
let anotherVariable = "World"; // declaring a variable using let
const constantVariable = "This value cannot be changed"; // declaring a variable using const

The choice between var, let, and const depends on the intended use of the variable. If a variable is only used within a specific block of code, it's recommended to use let or const to ensure that the variable does not leak outside of that block. If a variable is going to be re-assigned, let is the appropriate choice. If a variable should not be re-assigned, const is the better option. var can still be used in legacy code or in situations where its specific scoping behavior is desired.

Data Types

In JavaScript, there are seven primitive data types:

  1. Number: represents both integer and floating-point numbers.
  2. String: represents a sequence of characters, such as "hello world".
  3. Boolean: represents true or false values.
  4. Null: represents a deliberate non-value, and is a unique type that has only one value, which is null.
  5. Undefined: represents an uninitialized value, and is a unique type that has only one value, which is undefined.
  6. Symbol: represents a unique identifier, which can be used as an object property or a Map key.
  7. BigInt: represents integers that are larger than the range that can be represented by the Number type.

In addition to these primitive data types, there is also the object data type, which can hold key-value pairs and more complex data structures. Objects are not a primitive data type because they are mutable and can be modified.

Understanding the different data types in JavaScript is important for writing correct and efficient code, as well as avoiding common errors such as type coercion.

let price = 450.80; // number
let roll = 85;  // number 
let name = "Ruby"; //string
let is_done = false; //boolean

Null Vs Undefined

In JavaScript, null and undefined are both special values that represent absence of a value, but they are used in slightly different contexts:

  1. Undefined: This value means a variable has been declared but has not yet been assigned a value. It is also the default return value of functions that do not return anything. For example:
    let x;
    console.log(x); // Output: undefined
    
    function myfun() {
      // do something
    }
    console.log(myfun()); // Output: undefined
    
    1. Null: This value means a variable has been explicitly set to represent no value. It is often used to indicate the absence of an object or a value. For example:
      let x = null;
      console.log(x); // Output: null
      

Check Data Types

In javascript we can declare different types of data types such as number, string, boolean, array, objects etc. 'typeof' keyword can be used for find the data type of variables.

let a = 10; //number
let b = 10.73; //number
let c = "Hello world"; //string
let d = [10,20,30,40]; //array
let e = true; //boolean
let f= new Date(); //object

console.log(typeof(a))
console.log(typeof(b))
console.log(typeof(c))
console.log(typeof(d))