JavaScript is a versatile programming language primarily used to create dynamic and interactive content on web pages. It enables developers to implement features such as form validation, animations, and data manipulation.
Java | JavaScript |
---|---|
It is one of the most popular programming languages. It is an object-oriented programming language and has a virtual machine platform that allows you to create compiled programs that run on nearly every platform. Java promised, “Write Once, Run Anywhere | It is a light-weighted programming language (“client-side scripting language”) for developing interactive web pages. It can insert dynamic text into the HTML elements. JavaScript is also known as the browser’s language. |
Values represent data, while variables store these values for reuse. You can declare variables using let
, const
, or var
:
let age = 25;
const name = "John";
var isStudent = true;
JavaScript has several data types, categorized as:
- Primitive:
- Numbers.
- Strings.
- Booleans.
- Undefined.
- Null.
- BigInt.
- Symbol.
- Non-Primitive:
- Objects.
- Arrays.
let num = 10; // Number
let x; // undefined
let text = "Hello"; // String
let isTrue = false; // Boolean
let obj = {}; // Object
symbole used for comments in javaScript is // Single line
for single line and /* Multi-lines */
for multi-lines
let
: Block-scoped, mutable.const
: Block-scoped, immutable (value cannot be reassigned).var
: Function-scoped, mutable (avoid using it in modern code).
let count = 10;
const PI = 3.14;
var total = 0; // Older syntax
Operators perform operations on values:
- Arithmetic:
+
,-
,*
,/
,%
- Assignment:
=
,+=
,-=
- Comparison:
===
,>
,<
- Logical:
&&
,||
,!
let sum = 10 + 5; // 15
let isGreater = 10 > 5; // true
Operator precedence determines the execution order in complex expressions. Use parentheses to control precedence.
let result = 10 + 5 * 2; // 20, multiplication first
let corrected = (10 + 5) * 2; // 30
Conditional statements execute code blocks based on conditions:
if (age > 18) {
console.log("Adult");
} else if (age == 18) {
console.log("you are eighteen");
} else {
console.log("Minor");
}
Type conversion explicitly changes types, while coercion is implicit:
let num = "5";
console.log(Number(num)); // 5
console.log("5" + 10); // "510" (coercion)
Boolean logic uses &&
(AND), ||
(OR), and !
(NOT). Equality operators include ==
(loose equality) and ===
(strict equality):
console.log(true && false); // false
console.log(10 === "10"); // false
- Expressions produce values:
5 + 5
. - Statements perform actions:
if (true) {}
.
let str = "JavaScript";
-
charAt(index)
Returns the character at the specified index.console.log(str.charAt(4)); // Output: S
-
charCodeAt(index)
Returns the Unicode value of the character at the specified index.console.log(str.charCodeAt(4)); // Output: 83
-
concat(string1, string2, ...)
Concatenates strings.console.log("Hello".concat(" ", "World!"," ","Meefr" )); // Output: Hello World! // same as console.log("Hello " + "World! " + "Meefr")
-
includes(substring, start)
Checks if a string contains a specified substring.console.log(str.includes("Script")); // Output: true
-
indexOf(substring, start)
Returns the index of the first occurrence of the substring, or -1 if not found.console.log(str.indexOf("a")); // Output: 1
-
lastIndexOf(substring, start)
Returns the index of the last occurrence of the substring.console.log(str.lastIndexOf("a")); // Output: 3
-
toUpperCase()
Converts a string to uppercase.console.log(str.toUpperCase()); // Output: JAVASCRIPT
-
toLowerCase()
Converts a string to lowercase.console.log(str.toLowerCase()); // Output: javascript
-
substring(start, end)
Extracts characters between two indices.console.log(str.substring(0, 4)); // Output: Java
-
slice(start, end)
Extracts part of a string, supports negative indices.console.log(str.slice(-6)); // Output: Script
-
split(separator, limit)
Splits a string into an array based on a separator.console.log("Hello World".split(" ")); // Output: ["Hello", "World"]
-
trim()
Removes whitespace from both ends of a string.console.log(" Hello World ".trim()); // Output: Hello World
-
trimStart()
/trimEnd()
Removes whitespace from the start or end of a string.console.log(" Hello".trimStart()); // Output: Hello
-
padStart(targetLength, padString)
Pads the beginning of the string to a specified length.console.log("123".padStart(5, "0")); // Output: 00123
-
padEnd(targetLength, padString)
Pads the end of the string.console.log("123".padEnd(5, "0")); // Output: 12300
-
replace(pattern, replacement)
Replaces the first match of a pattern with a replacement.console.log("Hello World".replace("World", "JavaScript")); // Output: Hello JavaScript
-
replaceAll(pattern, replacement)
Replaces all matches of a pattern.console.log("aaa".replaceAll("a", "b")); // Output: bbb
-
match(regex)
Need to know regex !
Matches a string against a regular expression and returns the matches.
console.log("JavaScript".match(/a/g)); // Output: ["a", "a"]
-
search(regex)
Searches for a match using a regular expression and returns the index of the match.console.log("JavaScript".search(/S/)); // Output: 4
- Template literals use backticks for string interpolation and multi-line strings.
let name = "John"; console.log(`Hello, ${name}!`); // Output: Hello, John!
- Extension Retriever
let x = "image.png" // method 1 not generic let ext = x.slice(-3) console.log(ext); // method 2 generic ext = x.split(".") const index = ext.length - 1 console.log(ext[index])
Feature | Function | Arrow Function |
---|---|---|
Syntax | Uses the function keyword. |
Uses => arrow syntax. |
this Binding |
Dynamic (depends on how called). | Lexical (inherits from surrounding scope). |
arguments Object |
Available. | Not available; use rest parameters instead. |
Constructor Support | Yes, can be used as constructors. | No, cannot be used as constructors. |
Method Suitability | Ideal for object methods. | Ideal for callbacks and non-method functions. |
Implicit Return | Requires return for output. |
Supports implicit return for single-line expressions. |
-
Function
function add(a, b) { return a + b; }
-
Arrow Function
const add = (a, b) => a + b; // or const add = (a,b) => { return a+b };
-
Function
const obj = { value: 10, method: function () { console.log(this.value); // Refers to obj } }; obj.method(); // Output: 10
-
Arrow Function
const obj = { value: 10, method: () => { console.log(this.value); // Refers to outer scope } }; obj.method(); // Output: undefined
-
Function
function Person(name) { this.name = name; } const person = new Person("John"); console.log(person.name); // Output: John
-
Arrow Function
const Person = (name) => { this.name = name; }; const person = new Person("John"); // Error: Person is not a constructor
Functions encapsulate reusable code:
function greet(name) {
return `Hello, ${name}!`;
}
const arrowGreet = (name) => `Hello, ${name}!`;
- Arrays store ordered collections:
let fruits = ["Apple", "Banana"];
- Objects store key-value pairs:
let person = { name: "Alice", age: 30 };
Loops repeat code until a condition is met:
- for loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
- while loop:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
- Solve this Tutorial: Tutorial Link
- Solve this Practice : Practice Link
- Solve this problems Problem Solving