Return Multiple Values from Javascript Function

A Javascript function returns one and only one value. function sum(x, y) { return 3 + 39; } let life = sum(3, 39); console.log(life); // 42 I bet you already knew that not returning a value from a function, would return ‘undefined’. Anywho.. you are in a pickle if you want to return multiple values. There is no return(sum, diff);. However, a lot of problems need multiple ’things’ from the function output. What we do is to create an object in the function, populate it with multiple values and return the object to caller. The object packages all required variables. ...

Logical Operators in Javascript

AND (&&), Not (!), OR (||) are logical operators in Javascript. You will see that they are the only operators that make up for the entirety of one’s life experiences. Logical operators evaluate two values or expressions, and provide a true or false result. Let us see some examples to know the rules of the game. AND Only trues get along with each other to provide a true result - when dealing with an AND operation. ...

Comparison Operators in Javascript

Equals (==), not equals (!=), greater than (>), lesser than (<) - Javascript comparison operators cannot get more simple. Let’s dissect these operators a bit. Comparison operators are the ones that you use to.. well, compare. For example: let numCoffee = 3; if (numCoffee < 2) console.log("too few"); else if (numCoffee == 2) console.log("just right"); else console.log("crazy"); // output: crazy The expression within brackets is evaluated to true or false based on operator precedence. A succinct representation of the above totally real-world problem will be - ...

Objects and Props in Javascript

Know how you can use a Javascript object and its props. Javascript objects have properties or props. As you would have seen in the post about objects and primitives, object operations are quite easy. Though objects can mean more than a few things in Javascript, we will limit discussion of an object to the ** real real ** object. ..i.e., let obj = { i: 1 }; You should know a few things about this object to make your life much easier. ...

Objects and Primitives in Javascript

Get to know all the practical knowledge about Javascript primitives and objects. In JavaScript, objects are king. If you understand objects, you understand JavaScript. W3 Schools Everything except primitives is an object in Javascript. A couple of days back, we were talking about Javascript having the following primitives: bool, number, string, undefined and null. Primitives are quite easily defined and accessed - var name = "Mr. Anderson"; var x = 1; var universe = null; Objects on the other hand.. can also be easily defined and accessed. ...

Type Casting in Javascript

Don’t know how to type? Get a typewriter. I always wanted to say that. But, if indeed you don’t know types - you can always head over to Javascript types to know more**. So, you know there are types. And, you also know that there can be many a problem in the lousy world of loose typing. (I just felt like the next S waxing such eloquence). The only choice to make here is to know more about how to convert from one type to other, and remain type-safe. And, that will quickly become second nature to your programming style. ...

Typing Javascript

This one thing I never said ever - “once you type, you cannot un-type”. Yep, I make up words and compete on how to level up in my eating my own words. Even if I allegedly uttered those words (which are totally unoriginal anyway), I happily don’t abide by them. I continue to use Javascript without types with all fervour (and have had a stint with typeless / loosely-typed scripting languages). ...

The v-model Magic

In a previous post, we saw the power of bindings in Vue. v-model is a powerful way to bind variables and UI rendering in Vue. Let us look at a bit more of v-model in this post. Remind me what is v-model about? In its simplest form.. <input type="text" v-model="myName" label="Your name:"> .. and it doesn’t quite get more complicated than that on its own. When you change ‘myName’ manually, through another component, or from the back-end, the variable value changes for sure but will also trigger off a change to UI. The user will see near instantaneous change to a specific UI element without any page refresh. ...

Vue Reactivity and Binding

The power of Vue is in making reactive components child’s play. Well, that is true if the child in question is a 8-year old nephew who knows the basics of Javascript - you know what I mean. Consider an example that I have used before - <template> <div class="hello"> <h1>{{ msg }}</h1> <p> <button @click="sayHello">Hello</button> <span v-if="showHello">&nbsp;World!</span> </p> <p> <input type="text" v-model="myName" label="Your name:" /> </p> <p v-if="myName">Hello to you too.. {{ myName }}</p> </div> </template> <script> export default { name: "HelloWorld", data() { return { myName: null, showHello: false, }; }, methods: { sayHello() { console.log("hello world"); this.showHello = !this.showHello; }, }, props: { msg: String, }, }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h3 { margin: 40px 0 0; } a { color: #42b983; } </style> ‘HelloWorld’ is a typical Vue component. ...

Reverse a String in Javascript

Oh.. why? Why should we work on theoritical algo problems when we could develop the next Instagram or Salesforce.com? Because, son - you have to learn the different paths to glory. Why take the longer path when it can be shorter? That aside: let us look at multiple ways of reversing a string in Javascript. The best and easiest way Convert to array and back. const txt = "abcxyz"; console.log(txt.split("").reverse().join("")); Prove that I know ‘modern’ functions Aka - use reducer to reverse. ...