In JavaScript, .length
is a commonly used property that returns the number of elements in an array, the number of characters in a string, or the number of arguments in a function, depending on the context.
1. String Length
Returns the number of characters in a string:
let str = "Hello, world!";
console.log(str.length); // Output: 13
2. Array Length
Returns the number of elements in an array:
let arr = [1, 2, 3, 4];
console.log(arr.length); // Output: 4
You can also change the length of an array:
arr.length = 2;
console.log(arr); // Output: [1, 2]
3. Function Length
Returns the number of expected arguments in a function:
function myFunc(a, b, c) {}
console.log(myFunc.length); // Output: 3
In JavaScript, variables are used to store data values. You can declare a variable using one of three keywords:
1. var
(Old, avoid if possible)
var name = "Alice";
- Function-scoped
- Can be redeclared and updated
- Gets hoisted (but initialized with
undefined
)
2. let
(Preferred for variables that change)
let age = 25;
age = 26; // OK
- Block-scoped
- Can be updated, but not redeclared in the same scope
3. const
(Preferred for constants)
const pi = 3.14159;
// pi = 3; // Error
- Block-scoped
- Cannot be updated or redeclared
- For arrays/objects: you can change contents, but not reassign
const person = { name: "Alice" };
person.name = "Bob"; // Allowed
// person = { name: "Charlie" }; // Error
Best Practice
- Use
**const**
by default - Use
**let**
only when you know the variable will change - Avoid
**var**
unless you’re dealing with old codebases
An array in JavaScript is a special variable used to store multiple values in a single variable. Arrays are zero-indexed and can hold elements of any data type, including numbers, strings, objects, or even other arrays.
Creating an Array
// Using array literal
let fruits = ["apple", "banana", "cherry"];
// Using the Array constructor
let numbers = new Array(1, 2, 3, 4);
Accessing Elements
console.log(fruits[0]); // "apple"
console.log(fruits[2]); // "cherry"
Modifying Elements
fruits[1] = "blueberry";
console.log(fruits); // ["apple", "blueberry", "cherry"]
Example
let colors = ["red", "green", "blue"];
colors.push("yellow");
console.log(colors); // ["red", "green", "blue", "yellow"]
In JavaScript, the Math
object and Math.random()
function are used for performing mathematical operations and generating random numbers.
Math
Object in JavaScript
The Math
object provides properties and methods for mathematical constants and functions. It is not a constructor, so you don't use new Math()
.
Math.random()
in JavaScript
Math.random()
returns a floating-point number between 0 (inclusive) and 1 (exclusive).
Examples
// Random number between 0 and 1
let rand = Math.random();
// Random number between 0 and 100
let rand100 = Math.random() * 100;
// Random integer between 1 and 10
let randInt = Math.floor(Math.random() * 10) + 1;
console.log(rand, rand100, randInt);
Random Integer Function
You can create a reusable function to get a random integer in a range:
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(5, 15)); // Random integer between 5 and 15
Top comments (0)