JavaScript Datatypes and Variables - Important Points
JavaScript Datatypes and Variables - MCQ
JavaScript is a dynamically-typed language, meaning that variables do not have to be declared with a specific data type. Instead, data types are automatically assigned based on the value assigned to a variable. Here, we will discuss different data types and variables available in JavaScript.
Data Types
Numbers: Numbers in JavaScript are represented by the number data type, which can be used to store both integers and floating-point numbers. Examples include 1, 2.5, -3, etc.
let num1 = 42; // An integer
let num2 = 3.14; // A floating-point number
Strings: Strings in JavaScript are represented by the string data type, which is used to store text. Examples include "hello", "world", "123", etc.
let str1 = "Hello";
let str2 = 'World';
Booleans: Booleans in JavaScript are represented by the boolean data type, which is used to store true/false values.
let isTrue = true;
let isFalse = false;
Undefined: The undefined data type is used to represent variables that have not been assigned a value.
let undefVar;
console.log(undefVar); // Output: undefined
Null: The null data type is used to represent a variable with no value or an empty value.
let nullVar = null;
console.log(nullVar); // Output: null
Objects: Objects in JavaScript are used to store collections of data and can contain multiple properties and methods.
let person = {
name: "John",
age: 30,
address: "123 Main St"
};
Arrays: Arrays in JavaScript are used to store collections of data, such as a list of numbers or a list of strings.
let numbers = [1, 2, 3, 4, 5];
let fruits = ["apple", "banana", "orange"];
Variables
Variables in JavaScript are used to store data and can be declared using the var, let, or const keyword. The var keyword was used to declare variables in earlier versions of JavaScript but has largely been replaced by let and const. Variables declared using let and const are block-scoped, meaning they are only accessible within the block of code in which they are declared.
var: Variables declared using var are function-scoped, meaning they can be accessed within the entire function in which they are declared.
let: Variables declared using let are block-scoped, meaning they are only accessible within the block of code in which they are declared.
const: Variables declared using const are also block-scoped and cannot be reassigned once they are declared.
// var example
function varExample() {
var x = 10;
if (true) {
var x = 20;
console.log(x); // Output: 20
}
console.log(x); // Output: 20
}
// let example
function letExample() {
let x = 10;
if (true) {
let x = 20;
console.log(x); // Output: 20
}
console.log(x); // Output: 10
}
// const example
const MY_CONSTANT = "Hello World";
console.log(MY_CONSTANT); // Output: Hello World
In conclusion, understanding the different data types and variables available in JavaScript is essential for writing effective code. By having a clear understanding of these concepts, beginners can write cleaner and more efficient code that can easily be understood and maintained.