JavaScript is referred to as a dynamic language because it allows for dynamic typing. This means that the same variable can be used to hold different data types. For instance, we can assign a number to a variable and then later assign a string to the same variable. Here’s an example:
var dynamicVariable = 5; // Number
console.log(dynamicVariable); // Output: 5
dynamicVariable = "Hello, World!"; // String
console.log(dynamicVariable); // Output: Hello, World!
In this example, the variable dynamicVariable first holds a number, then is reassigned to hold a string. This flexibility is part of what makes JavaScript a dynamic language.
JavaScript determines data types automatically when a variable is declared and assigned a value. Data types are distinguished between primitive types and object types. Primitive types in JavaScript include Number, String, Boolean, Undefined, Null, and Symbol. If a variable is not one of these types, it is considered an object. Here’s an example:
let num = 25; // Number
let str = "Hello"; // String
let bool = true; // Boolean
let nothing = undefined; // Undefined
let empty = null; // Null
let sym = Symbol("sym"); // Symbol
let obj = {name: "John"}; // Object
console.log(typeof num); // Output: number
console.log(typeof str); // Output: string
console.log(typeof bool); // Output: boolean
console.log(typeof nothing); // Output: undefined
console.log(typeof empty); // Output: object (null is considered an object in JavaScript)
console.log(typeof sym); // Output: symbol
console.log(typeof obj); // Output: object
In JavaScript, typeof is an operator that returns a string indicating the type of the unevaluated operand, i.e., the type of the data (not the variable itself). When used with a function, typeof returns the string “function”. Here’s an example:
function greet() {
return "Hello, World!";
}
console.log(typeof greet); // Output: function
In this example, typeof is used with the function greet, and it returns the string “function”, indicating that the type of greet is a function.
In JavaScript, you can check the data type of a variable using the typeof operator. This operator returns a string indicating the type of the unevaluated operand. Here’s an example:
let num = 25; // Number
let str = "Hello"; // String
let bool = true; // Boolean
console.log(typeof num); // Output: number
console.log(typeof str); // Output: string
console.log(typeof bool); // Output: boolean
In this example, typeof is used to check the data type of the variables num, str, and bool.
JavaScript has seven fundamental data types:
- Number: Any numerical value, including decimals and negatives. For example,
let num = 25;
- String: A sequence of characters enclosed in quotation marks. For example,
let str = "Hello";
- Boolean: Represents either
true
orfalse
. For example,let bool = true;
- Undefined: A variable that has been declared but has not yet been assigned a value is undefined. For example,
let nothing;
- Null: Represents the intentional absence of any object value. For example,
let empty = null;
- Symbol: A unique and immutable data type that is often used as an identifier for object properties. For example,
let sym = Symbol("sym");
- Object: Anything that is not a primitive type (Number, String, Boolean, Null, Undefined, Symbol) is an object. For example,
let obj = {name: "John"};
In JavaScript, undefined is a primitive data type. A variable that has been declared but not assigned a value is of type undefined. This is different from a variable not being declared at all, which will result in an error if you try to access it. Here’s an example:
let notAssigned;
console.log(notAssigned); // Output: undefined
console.log(typeof notAssigned); // Output: undefined
console.log(notDeclared); // Output: Error: notDeclared is not defined
In the first part of the example, notAssigned is declared but not assigned a value, so its value is undefined and its type is undefined. In the second part, notDeclared is not declared at all, so trying to access it results in an error.
In JavaScript, null is a primitive data type that represents the intentional absence of any object value. It is often used when you want to explicitly make a variable empty. Here’s an example:
let empty = null;
console.log(empty); // Output: null
console.log(typeof empty); // Output: object (interesting fact: null is considered an object in JavaScript)
When you declare a variable without using the var keyword, it becomes a global variable, regardless of where it was declared. This is because JavaScript treats it as if it was declared at the top-most scope. Here’s an example:
function exampleFunction() {
noVar = "I'm a global variable!";
}
exampleFunction();
console.log(noVar); // Output: I'm a global variable!
In this example, noVar
is declared inside exampleFunction
without the var
keyword. Despite this, it can be accessed outside the function because it’s treated as a global variable.
However, using variables without declaring them is considered a bad practice because it can lead to unexpected results and hard-to-debug code. Also, in strict mode ("use strict";
), assigning a value to an undeclared variable, a read-only property, or a non-writable global variable, will throw an error.