JavaScript Exam Summary
1. Introduction to JavaScript
Definition: A scripting language for creating interactive web pages.
Purpose:
- Handle events and user interactions.
- Perform client-side calculations.
- Manipulate HTML and CSS dynamically.
Execution: Runs in the client’s browser and can be disabled by users.
2. Where to Write JavaScript
Inline: Within HTML elements using the 'onEvent' attribute (e.g., onClick).
Internal: Inside <script type="text/javascript" language="JavaScript"> </script> tags in
the <body> (9ad mat7eb mn marra) or <head> :
External: In separate .js files linked using <script language=”javascript” src="file.js">
</script>. Either fl head or e5er l body:
3. Syntax and Basics
Case-sensitive, La casse est importante: Keywords and identifiers are case-sensitive.
(majuscule does NOT= minuscule)
Comments:
- Single-line: // Comment.
- Multi-line: /* Comment */.
Variables:
- Declared using var, let, or const.
- Dynamic typing: Variable type is determined at runtime.
Example:
let name = "John";
const age = 25;
var prenom; -- undefined
Operators:
- Arithmetic: +, -, *, /, %.
- Comparison: ==, ===, !=, >, <.
- Logical: &&, ||, !.
4. Data Types
Primitive Types: Number, String, Boolean, Undefined, Null.
Complex Types:
- Array: let fruits = ["apple", "banana", "cherry"];.
- Object: let person = {name: "Alice", age: 30};.
Type Checking: Use typeof to check variable types.
5. Control Structures
Conditional Statements:
if (condition) { ... } else { ... }
switch (value) { case 1: ... break; default: ... }
Loops:
- for, while, do...while.
Example:
for (let i = 0; i < 5; i++) { ... }
6. Functions
Declaration: function name(parameters) { ... }.
Arrow Functions:
Example:
const greet = (name) => `Hello, ${name}!`;
Built-in Functions:
- alert(), confirm(), prompt().
- document.write(), console.log().
- isNaN(), parseInt(), parseFloat().
7. DOM Manipulation
Selectors:
- document.getElementById("id").
- document.querySelector(".class").
Events:
- Inline: <button onClick="alert('Clicked')">Click Me</button>.
- Add Listener: element.addEventListener("click", function);
Modify Elements:
Example:
document.getElementById("demo").innerHTML = "Text";
document.body.style.backgroundColor = "blue";
8. Objects and Methods
Window Object: Global object providing functions like alert(), prompt().
Document Object: Represents the HTML content and provides methods for manipulation.
Example:
document.title = "New Title";
10. Forms and Validation
Access Form Elements:
Example:
let name = document.forms["nameForm"]["nameInput"].value;
Validation Example:
if (name === "") { alert("Name cannot be empty"); }
Expression Explanation Result
Compares only values. "123" is coerced to a number
"123" == 123 true
before comparison.
Compares values and types. "123" (string) is not the
"123" === 123 false
same type as 123 (number).
0 == false Compares only values. false is coerced to 0. true
Compares values and types. 0 (number) is not the
0 === false false
same type as false (boolean).
Special case in JavaScript: null and undefined are
null == undefined true
considered equal only with ==.
Compares values and types. null and undefined are
null === undefined false
different types.
String "123" is coerced to a number 123. Compares
"123" > 100 true
numerically.
"123abc" cannot be coerced to a number, so it
"123abc" > 100 false
remains a string. Comparison fails.
isNaN("abc") "abc" is not a number, so isNaN() returns true. true
"123" is a valid number (after coercion), so isNaN()
isNaN("123") false
returns false.
isNaN(123) 123 is already a number, so isNaN() returns false. false
123
Number("123") Explicitly converts "123" to the number 123.
(number)
"abc" cannot be converted to a number, so it returns
Number("abc") NaN
NaN.
" " == 0 Empty string is coerced to 0. true
Strict equality: Empty string (string) and 0 (number)
" " === 0 false
are different types.
Extracts valid numeric part (123.45) and ignores the 123.45
parseFloat("123.45abc")
invalid part (abc). (number)
parseFloat("abc") Cannot extract a number, so it returns NaN. NaN
Expression Explanation Result
Compares lexicographically (alphabetically) as both
"abc" > "def" false
are strings. "abc" is smaller than "def".
"123abc" cannot be fully coerced to a number.
"123abc" == 123 false
Comparison fails.
0 == "" Empty string ("") is coerced to 0. true
null > 0 null is coerced to 0 for numeric comparison. false
undefined cannot be coerced to a meaningful
undefined > 0 false
number for comparison.