JavaScript: Object Orientation, Syntax, Primitives, Operations, and I/O
Object Orientation and JavaScript
JavaScript supports object-oriented programming (OOP), but it is prototype-based rather than
strictly class-based like Java or C++. In JavaScript:
Objects are collections of properties (key-value pairs) that can contain data and functions
(methods).
Constructors serve as templates for creating objects; they define the "shape" of objects
and their methods.
Prototypes allow objects to inherit properties and methods from other objects, enabling
inheritance. When a method is defined on a constructor's prototype, all objects created from
that constructor share the method.
The prototype chain is JavaScript’s mechanism for inheritance, allowing objects to inherit
features from other objects [1] [2] .
Example of a constructor and prototype:
function Person(name) {
this.name = name;
}
Person.prototype.introduce = function() {
console.log("Hi, I'm " + this.name);
};
let alice = new Person("Alice");
alice.introduce(); // Hi, I'm Alice
General Syntactic Characteristics
JavaScript syntax is similar to C and Java:
Case-sensitive.
Statements end with semicolons (optional but recommended).
Curly braces {} define code blocks.
Variable and function names use letters, digits, underscores, and dollar signs.
Uses familiar operators for arithmetic, comparison, and logic [3] .
Example:
let total = 5 + 3;
if (total > 5) {
console.log("Total is greater than 5");
}
Primitives, Operations, and Expressions
Primitives
JavaScript has seven primitive data types, which are not objects and have no methods or
properties:
string
number
bigint
boolean
undefined
symbol
null
Primitives are immutable, though variables holding them can be reassigned [4] .
Operations and Expressions
Operators include arithmetic (+, -, *, /, %), assignment (=, +=, etc.), comparison (==, ===, !=,
>, <), logical (&&, ||, !), string concatenation (+), and more [5] [6] .
Expressions combine values, variables, and operators to produce a result. They can be
simple (let x = 5;) or complex (let y = (x * 2) + 7;).
Example:
let a = 10;
let b = 3;
let sum = a + b; // 13
let isEqual = a === b; // false
let isEven = (a % 2 === 0) ? true : false; // true
Screen Output and Keyboard Input
Screen Output
JavaScript can display output in several ways:
HTML Output:
document.getElementById("id").innerHTML = "Hello";
document.write("Hello"); (not recommended for modern web apps)
Dialog Boxes:
alert("Hello"); (popup message)
console.log("Hello"); (outputs to browser console) [7] [8] .
Keyboard Input
Prompt Dialog:
let name = prompt("Enter your name:"); (shows a dialog box for user input)
Confirm Dialog:
let result = confirm("Are you sure?"); (shows OK/Cancel, returns boolean) [8] .
Example:
let user = prompt("What is your name?");
alert("Welcome, " + user + "!");
Summary:
JavaScript blends object-oriented features (via prototypes and constructors) with a C-like
syntax, supports several immutable primitive types, and offers a rich set of operators and
expressions. Output is typically managed via the DOM, alerts, or the console, while basic input
uses prompt dialogs. These features make JavaScript a flexible and powerful language for web
development.
⁂
1. https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Advanced_JavaScript_o
bjects/Object-oriented_programming
2. https://www.freecodecamp.org/news/object-oriented-programming-javascript/
3. https://study.com/academy/lesson/javascript-syntax-overview-examples.html
4. https://developer.mozilla.org/en-US/docs/Glossary/Primitive
5. https://masteringbackend.com/hubs/javascript-essentials/operators-and-expressions
6. https://www.w3schools.com/js/js_operators.asp
7. https://www.w3schools.com/js/js_output.asp
8. https://www.startertutorials.com/ajwt/input-output.html