The indexOf() function in JavaScript

The indexOf() is a handy little function that finds the first occurance of a string inside a target string. It returns the start position of the string.

What is also useful (but not featured in the example below) is the optional second start argument which searches from the particular position with the first character being at 0 (i.e. indexOf("hello", 10) would start the search from position 10).

JavaScript

The push() function in JavaScript

It's super basic, but I figured since I needed to look it up I'd make a note of it here.  push() simply adds an item on to the end of an array. Something I'm sure any JavaScript developer will have to use quite a bit.

JavaScript

The slice() function in JavaScript

I also came across the slice() function for the first time in Javascript for the first time while on Chapter 6 in Eloquent Javascript. To be honest, I find slice() and split() sounding very similar – so I have to remember not to confuse them. I've worked with similar functions in other languages but as of writing I am still not 100% sure what is possible with slice(), so time to understand.

According to one of my favourite sources for javascript functions, w3schools summarises slice() as the following: "The slice() method returns the selected elements in an array, as a new array object."

So, lets see this in action:

JavaScript

The split() Function in Javascript

In Chapter 6 of Eloquent Javascript there is an example where I have seen the split() function for the first time. I thought I'd play around a little bit with it in a practical example so I could grasp it a little bit better.

The essential explanation of split() on w3schools.com is "The split() method is used to split a string into an array of substrings, and returns the new array." 

A practical example where you can use split() to reverse, or re-organise the order of words in a string, very useful for changing Walkie-talkie into the French version of the phrase.

JavaScript

Ternary Operators in Javascript

I was somewhat underwhelmed when I investigated tenary operators, as I had erroneously made the assumption that just like unary operators and binary operators, ternary operators would take various forms.

However, from what I can tell (at the time of posting) ternary operators in JavaScript always take a form similar to the following code snippet:

Essentially, if what is inside the braces preceding the ? is true; (burger="Big Mac'), then display the first argument; "It is a Big Mac", if not true, display the second argument; "It is not a Big Mac". Because the burger = Whopper, it will output "It is not a Big Mac".

For more information about ternary operators in JavaScript check out the following:

JavaScript

Binary Operators in Javascript

Binary operators are essentially where you process two objects, practical examples of binary operators follow:

Working through binary operators was actually surprisingly challenging for me – I realised there are a number of operators that I seldom use and didn't 100% (even 80%) understand how they worked. Probably the biggest concept I learnt about was == vs === and != vs !==. As you've probably worked out, I am no computer scientist – I try to focus on the practical side of things. So, I found several theory heavy pages on the web trying to explain the difference but I really found those kinds of explanations difficult – but I stumbled across this useful snippet in Douglas Crockford's highly praised Javascript: The Good Parts:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:
 
'' == '0'           // false
0 == ''             // true
0 == '0'            // true
 
false == 'false'    // false
false == '0'        // true
 
false == undefined  // false
false == null       // false
null == undefined   // true
 
' \t\r\n ' == 0     // true
 
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.
For those wanting to know more, I found the discussion at StackOverflow entitled JavaScript === vs == : Does it matter which “equal” operator I use? worthwhile.

I have also chosen not to include the following Binary Operators in the examples, as I really do not understand their meaning well enough at the time of this post. If I find it necessary I may post about them in the future:

^   //XOR
~   //Invert each bits
<<  //Move all bits onto the left
>>  //Move all bits onto the right
>>> //Move all bits onto the right and fill left end with 0
JavaScript

Unary Operators in Javascript

According to the examples provided by Ganderton on wiki.answers.com Unary operators are "operators that don't need another object, like : ++, +, –, -, !". So the following provides examples of what I am talking about.

While a++++aa––a made sense to me it took me a little while to understand what !a+a and -a actually did in a practical context. Hopefully the code example below will help you to understand. However, for further information on !a look at my stackoverflow question, for +a and -a check out my stackoverflow question and this useful section of an article on the Mozilla Development Network.

JavaScript

Unary, Binary and Ternary Operators in Javascript

I thought I was up to speed with operators, but I was scratching my head more than expected when I ran into the part in Eloquent Javascript (Chapter 6) where it explained how a ternary operator had been used.

While (I think) I understand the basic concept of a ternary operator – I wanted to make sure I understood operators in general a little bit more. While I have experience in different languages, I wanted to make sure I knew the fundamentals for operators in JavaScript. I decided to go back to basics and examine operators as a whole.

From what I can find out there are three different operators in JavaScript

With the goal of keeping each post to one example at a time (lets see how that goes hey?) – I have split the post up for each kind of operator. 

JavaScript

Lexical Scoping in Javascript

Mark Story's explanation on Lexical Scoping provides an excellent summary of how Lexical Scoping applies in javascript: "Lexical scoping is a fancy term that refers to a function remembering and preserving its state between and after executions."

A basic example of Lexical scoping should be clear by reviewing this example that has been modified from the one featured on Mark's blog:

JavaScript

Pure Functions in Javascript

Following on from the example of Side Effects in Javascript, a Pure Function in Javascript as described in Eloquent Javascript (Chapter 3) - "The defining properties of pure functions are that they always return the same value when given the same arguments, and never have side effects."

We can use the second example from the previous post (Side Effects in Javascript), as it:

  • Always return the same value when give the same arguments
  • Has no side effects

JavaScript