JavaScript Primer Overview
JavaScript Primer Overview
JavaScript supports 'for' and 'while' loops for iteration. A 'for' loop iterates a set number of times with a counter, like 'for (var i = 0; i < anArray.length; i++) { console.log(anArray[i]); }'. A 'while' loop continues as long as a condition is true, such as 'while (x < 10) { console.log(x++); }'. These loops facilitate repeated execution of code blocks.
In JavaScript, a function is defined using the 'function' keyword, followed by a name and parentheses for parameters. For example, 'function addOne(x) { return x + 1; }' defines a function 'addOne' that takes one argument 'x'. To call the function with an argument, use 'addOne(5)', which returns 6.
The 'conditional operator' in JavaScript, also known as the ternary operator, offers a shorthand for writing if-else statements. It uses the syntax 'condition ? expressionIfTrue : expressionIfFalse'. For instance, in the expression 'var b = (a > 0) ? "a is greater than zero" : "a is zero or less";', if 'a > 0' evaluates to true, 'b' is assigned "a is greater than zero"; otherwise, it is "a is zero or less".
In JavaScript exception handling, the 'try' block is used to encapsulate code that may throw an exception. If an exception occurs, control is transferred to the 'catch' block, where the error can be handled. The 'finally' block is always executed regardless of whether an exception was thrown or caught, typically used for cleanup operations.
JavaScript uses the 'typeof' operator to determine the type of a variable, which returns values like 'string', 'number', 'boolean', etc. For more precise type checking, particularly to differentiate between arrays and other object types, JavaScript uses 'Object.prototype.toString.call'. For example, 'Object.prototype.toString.call([1,2,3])' returns '[object Array]', allowing arrays to be distinguished from other objects like '[object Object]'.
JavaScript supports two types of comment syntax. Single-line comments use a double forward slash, for example, '// this is a single line comment'. Multi-line comments begin with '/*' and end with '*/', as in '/* This is a multi-line comment */'. These comment syntaxes allow developers to include explanatory notes or temporarily disable code.
The 'typeof' operator in JavaScript is used to identify the primitive data type of a variable, returning strings such as 'undefined', 'string', 'number', 'boolean', and 'object'. In contrast, 'instanceof' checks for object type by verifying if an object is an instance of a specific constructor. For example, '[] instanceof Array' returns true. 'typeof' is for basic type checking, while 'instanceof' is used for complex objects.
The 'forEach' method in JavaScript executes a provided function once for each array element, offering a clean and concise syntax for array iteration. For instance, 'var friends = ["mike", "roberto", "rodger"]; friends.forEach(function(eachName, index) { console.log(index + 1 + ". " + eachName); });' iterates over 'friends' and logs elements with their indices. This improves code readability over traditional for loops.
JavaScript's built-in numerical operators include '+' (addition), '-' (subtraction), '*' (multiplication), '/' (division), and '%' (modulo). These operators perform arithmetic operations, such as 'var a = 6; a = a + 1;' which increases 'a' from 6 to 7. Increment and decrement operations are simplified with 'a++' and 'a--'.
In Node.js, callbacks are extensively used to handle asynchronous operations, allowing the non-blocking event model to function efficiently despite having a single execution thread. A callback function is passed as an argument to another function, and is executed once the asynchronous operation completes. For example, using 'setTimeout' to log 'done' after a delay, as in 'setTimeout(function() { console.log('done'); }, 2000);', demonstrates how callbacks enable code to continue executing while waiting for timed events to complete.