JavaScript Language Overview
ECMAScript and JavaScript Relationship
ECMAScript serves as the specification standard for JavaScript, while JavaScript represents one implementation of this standard. The distinction arose when Netscape submitted JavaScript to ECMA International for standardization in 1996, leading to the ECMA-262 specification.
ECMAScript Version History
| Year |
Version |
Key Features |
| 1997 |
ES1 |
Initial release |
| 1999 |
ES3 |
Regular expressions, try/catch |
| 2009 |
ES5 |
Strict mode, JSON support |
| 2015 |
ES6 |
Clases, modules |
| 2016 |
ES7 |
Exponentiation operator, Array.includes |
JavaScript Implementation Components
- Core Language (ECMAScript)
- Document Object Model (DOM)
- Browser Object Model (BOM)
JavaScript Integration Methods
Inline Script
<script>
// JavaScript code here
</script>
External Script
<script src="script.js"></script>
Language Basics
Comments
// Single-line comment
/*
Multi-line
comment
*/
Variable Declaration
var userName = "John";
var userAge = 25;
ES6 Variable Declarations
let counter = 0;
const MAX_VALUE = 100;
Data Types
Dynamic Typing
var data; // undefined
data = 42; // number
data = "text"; // string
Number Type
var num1 = 3.14;
var num2 = 1000;
var num3 = 2e5; // 200000
String Type
var greeting = "Hello";
var target = "World";
var message = greeting + " " + target;
String Methods
| Method |
Description |
| length |
Returns string length |
| toUpperCase() |
Converts to uppercase |
| indexOf() |
Finds substring position |
| slice() |
Extracts substring |
| split() |
Splits into array |
Template Strings (ES6)
const name = "Alice";
const greeting = `Hello ${name}, welcome!`;
Boolean Type
var isActive = true;
var isComplete = false;
Arrays
var items = ["apple", 123, true];
console.log(items[0]); // "apple"
Array Methods
| Method |
Description |
| push() |
Adds element to end |
| pop() |
Removes last element |
| shift() |
Removes first element |
| forEach() |
Iterates through elements |
| map() |
Creates new array |
Type Checking
typeof "text" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
Operators
Arithmetic Operators
+ - * / % ++ --
Comparison Operators
== === != !== > < >= <=
Logical Operators
&& || !
Control Structures
Conditional Statements
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}
Switch Statement
switch(day) {
case 0: dayName = "Sunday"; break;
case 1: dayName = "Monday"; break;
default: dayName = "Other";
}
Loops
for (let i = 0; i < 5; i++) {
console.log(i);
}
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
Ternary Operator
var result = condition ? value1 : value2;
Functions
Function Declaration
function calculateSum(a, b) {
return a + b;
}
Function Expression
const multiply = function(a, b) {
return a * b;
};
Arrow Functions (ES6)
const square = x => x * x;
const add = (x, y) => x + y;
Immediately Invoked Function
(function() {
console.log("IIFE executed");
})();
Objects
Object Literal
const person = {
firstName: "Jane",
lastName: "Doe",
age: 30
};
Object Property Access
console.log(person.firstName);
console.log(person["lastName"]);
Object Iteration
for (let key in person) {
console.log(key + ": " + person[key]);
}
Built-in Objects
Date Object
const now = new Date();
console.log(now.toLocaleString());
JSON Handling
const jsonString = '{"name":"Bob","age":40}';
const obj = JSON.parse(jsonString);
const str = JSON.stringify(obj);
Regular Expressions
const pattern = /^[a-z]+$/i;
const isValid = pattern.test("Hello");
Math Object
Math.max(10, 20, 30); // 30
Math.random(); // Random number 0-1
Math.floor(3.7); // 3