[Go to site: main page, start]

0% found this document useful (0 votes)
4 views3 pages

JavaScript Basics Syntax

The document provides an overview of JavaScript basics, covering variables and data types, strings, numbers, arrays, and objects. It includes descriptions of various functions and methods associated with these data types, as well as examples of loops and solved problems. Additionally, it presents exercises for further practice on manipulating strings, arrays, and objects.

Uploaded by

flikzslayer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

JavaScript Basics Syntax

The document provides an overview of JavaScript basics, covering variables and data types, strings, numbers, arrays, and objects. It includes descriptions of various functions and methods associated with these data types, as well as examples of loops and solved problems. Additionally, it presents exercises for further practice on manipulating strings, arrays, and objects.

Uploaded by

flikzslayer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Basics – Arrays, Objects, Strings,

Numbers
I. Variables and Data Types
■ JavaScript variables can hold different data types: string, number, boolean, object, array, etc.
let name = "Mike"; // String
let age = 21; // Number
let active = true; // Boolean
let person = {name: "Mike", age: 21}; // Object
let scores = [80, 90, 100]; // Array

II. Strings
Function / Property Example Description
length [Link] Returns string length.
toUpperCase() [Link]() Converts to uppercase.
toLowerCase() [Link]() Converts to lowercase.
includes() [Link]('word') Checks if contains substring.
indexOf() [Link]('a') Returns index of substring.
slice() [Link](0,3) Extracts part of string.
substring() [Link](2,5) Extracts characters between indexes.
replace() [Link]('a','b') Replaces first match.
split() [Link](',') Splits string into array.
trim() [Link]() Removes whitespace.

III. Numbers
Function / Property Example Description
toFixed() [Link](2) Rounds to given decimals.
toString() [Link]() Converts number to string.
parseInt() parseInt('10') Converts string to integer.
parseFloat() parseFloat('10.5') Converts string to float.
isNaN() isNaN(value) Checks if value is NaN.
[Link]() [Link](4.6) Rounds to nearest integer.
[Link]() [Link](4.6) Rounds down.
[Link]() [Link](4.1) Rounds up.
[Link]() [Link]() Returns random number 0–1.
[Link]() [Link](1,5,3) Returns largest number.

IV. Arrays
Method Example Description
push() [Link](5) Adds to end.
pop() [Link]() Removes last element.
shift() [Link]() Removes first element.
unshift() [Link](1) Adds to start.
concat() [Link](arr2) Merges arrays.
join() [Link](',') Joins elements into string.
slice() [Link](1,3) Copies part of array.
splice() [Link](1,2) Removes/replaces elements.
indexOf() [Link](3) Finds position of value.
includes() [Link](4) Checks if contains value.
map() [Link](x=>x*2) Returns new array by mapping.
filter() [Link](x=>x>5) Filters array elements.
reduce() [Link]((a,b)=>a+b) Reduces array to one value.
forEach() [Link](x=>[Link](x)) Loops through elements.

V. Objects
let user = {name: "Mike", age: 21};
[Link]; // Access property
user["age"]; // Bracket access
[Link] = "Developer"; // Add property
delete [Link]; // Remove property
[Link](user); // Get keys
[Link](user); // Get values
[Link](user); // Get key-value pairs

VI. Loops
for(let i=0; i<5; i++){ [Link](i); }
for(let key in obj){ [Link](key, obj[key]); }
for(let val of arr){ [Link](val); }
[Link](v => [Link](v));

VII. Solved Examples


Example 1: Find the sum of all numbers in an array.
let arr = [1,2,3,4];
let sum = [Link]((a,b)=>a+b);
[Link](sum); // 10
Example 2: Count vowels in a string.
let str = "Hello World";
let vowels = [Link]().split('').filter(c=>'aeiou'.includes(c)).length;
[Link](vowels);
Example 3: Create and print object details.
let car = {brand: "BMW", model: "X5", year: 2023};
for(let key in car){ [Link](key + ": " + car[key]); }
VIII. Exercises
1. Reverse a string without using reverse().
2. Find the largest number in an array.
3. Create an object with student grades and calculate the average.
4. Replace all spaces in a string with '-'.
5. Use filter() to get even numbers from an array.

You might also like