Javascript (JS) is one of the most used programming languages in the world today and this is due to it being one of the core technologies of the web. It also is one of the most versatile languages as it can be used for the web, data science, gaming, servers, desktop applications, etc. In today’s blog post, I would be highlighting some concepts of JS I have learnt so far.
"JavaScript is the only language that I'm aware of that people feel they don't need to learn before they start using it." — Douglas Crockford
COMMENTS
The first thing I would be talking about is commenting. Comments are words written in your code that JS would totally ignore. They are a way to explain your code to others and even to your future self. There are two ways to comment: the in-line comment and the multi-line comment.
The in-line comment is initiated with the use of two forward-slashes like so //
and they are is used at the start of the line which contains your comment. The multi-line comment, on the other hand, is started with a forward-slash and asterisk and ended with an asterisk and a forward slash like so /* */
.
// This is an in-line comment.
/* This is how a multi-line
comment is written.*/
ARRAY NESTING
An array is a special variable used to collect values in a list. It is formed using square brackets [ ]
with values (elements) inside it being separated using a comma. Arrays are even more special in that they can contain any type of data; integers, booleans, floats, strings, null, objects, etc.
var myArray = ["my", "array"]; // A simple Array
Nesting simply means to put something within another thing and with arrays in JS, there is no difference. Array nesting involves putting an array inside another and this is especially useful when working with matrices.
// myNestedArray contains two arrays
var myNestedArray = [
["banana", "strawberries"], [22, 7.5]
];
GLOBAL VS LOCAL SCOPE
The use of var
in declaring a Javascript variable can change the variable's scope i.e it could be global or local depending on its position.
A variable will have global scope when it is declared in either two ways: outside a function or when it is declared without var
. The global scope means the variable will be accessible to all functions in the code. i.e it can be visible from anywhere in the code.
globalScopeVariable = 54; // This is a globally scoped variable
function example() {
one = 22; // This is also a globally scoped variable
}
A locally scoped variable is formed when a variable is declared inside a function; they are initialised/created when a function starts and deleted when the function is completed.
function localScope() {
var localScopeVariable = “nicole” // Local scope variable
}
Can Global and local scope be used simultaneously in a code? Yes! Although the local scope always takes precedence.
var seeThis = 52; // Global scope
function again() {
var num2 = 44; // Local scope
}
CONDITIONAL OPERATOR
A conditional operator is also known as a ternary operator. It is used in evaluating statements that result in boolean values (true or false) and is the only JS operator that takes three operands: a condition to evaluate, an action to perform if the condition is true, and an action to perform if the condition evaluates to false. The first operand is followed by a question mark (?) and the second by a colon (:). It is used as a short form of the if-statement in JS. It executes if true. The third operand is the else-statement which execute when the first operand when false.
// A conditional operator has the form:
condition ? expression-if-true : expression-if-false;
For example, the function below:
function lessThan(y, z) {
if (y < z) {
return "y is lesser";
}
else {
return "z is lesser";
}
}
Can be written as:
function lesserThan(y, z) {
return (y < z) ? "y is lesser" : "z is lesser";
}
lesserThan(4, 5); // Output will be "y is lesser "
I have been having a great time learning about JS and most of my learnings are being done on freeCodeCamp. To learn more about Javascript or begin your web development journey, you can visit the link. Thanks for reading through. Please like, comment and feel free to check my previous posts too.