Learning JavaScript Strings

Learning JavaScript Strings, Booleans & Null (Part 2)

Learning JavaScript Strings, Booleans & Null: Strings are another fundamental data type representing a string (or sequence) of characters. Strings are used in programming to represent text values. In the previous class, we learned introduction to Javascript.

Let us continue with Strings; Strings are created by enclosing text in a single (“) or double (“”) quotation mark.

Learning JavaScript Strings: Strings Operation

JavaScript Strings

JavaScript strings are for storing and manipulating text. They are zero or more characters written inside quotes.

let name = "Cristiano Ronaldo";

You can use single or double quote based on your preference

let carType1 = "Tesla";  // Double quotes
let carType2 = 'Muscle Camry';  // Single quotes

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string. this means that if you begin with double qoutes, you must not qoute anything inbetween with a double qoute , you must use single qoute and vice versal.

See example below

let club1 = "Manchester football club";
let club2 = "Chelsea 'football' club";
let club3 = 'Arsenal "football" club';

Escape Character

Because strings must be placed between quotes, JavaScript will interpret this string incorrectly:

let text = "We are the so-called "Vikings" from norway.";

I am sure, I have explained the reason above. The string will be chopped to “We are the so-called ” because the opening quotation of Vikings was treated as the closing quotation of the entire string. How do we prevent this?

The backslash escape character may be used to prevent this issue.

The escape character backslash (/) converts special characters into string characters:

let text = "We are the so-called "Vikings" from norway.";

String Length

To find the length of a string, use the built-in length property:

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZZ";
let length = text.length;

Check it out:

See the Pen
Untitled
by aptLearn (@aptlearn)
on CodePen.40347

Strings are another basic data type that represents a series (or string) of characters. In programming, strings are used to represent the value of texts.

Strings Operation

String operations are confined. For example, a string cannot be subtracted, multiplied, or divided by another string. In contrast, the + operator may be used to concatenate (combine) strings.

Booleans

A computer, at its most basic, is a series of on and off switches, a collection of 0s and 1s that flip back and forth to, well, compute stuff.

The concept of using binary numbers to describe information (0 and 1, true and false) is so important in computing that Javascript, like most other programming languages, has a boolean type devoted to these values.

Boolean values can be created by assigning a value of true or false to a variable.

let veryTrue = true;
let notTrue = false;

Booleans from comparison operators

Boolean values can also be created by using comparison operators, such as:

  • Greater Than – >
  • Less Than – <
  • Greater Than or Equal To – >=
  • Less Than or Equal To – <=

console.log(101 > 100);
console.log(101 < 100);
console.log(10 >= 10);
console.log(20 <= 10);

If you run this into JavaScript, your output will be

In the next chapter, we will study equality operators

JavaScript Functions

After we’ve mastered the essential data types and operations, we should try to make our scripts more interesting. This will be performed by tinkering with the execution of the Javascript programme.

A JavaScript function is executed when “something” invokes it (calls it); check the below example.

function myFunction(p1, p2) {
  return p1 + p2;   // The function returns the product of p1 and p2
}

See the Pen
Untitled
by aptLearn (@aptlearn)
on CodePen.40347

Functions allow us to repeat tasks that involve a similar sequence of steps. You’ve already seen the console.log() function, which allows us to predictably log some output to the console.

Let’s look at an example of a function definition that finds the sum of two numbers:

let sum = function(x, y){
  return x + y;
}

We start by creating a variable, which we then define as a function by using the function(){} syntax.

Within the parentheses () are arguments, which are values that are used or manipulated by the function in some manner. A function may also be called with no parameters or arguments. If you have numerous parameters, separate them with a comma.

Within the curly brackets ({}) is the code that the function will perform. If you intend the function to return some value, it should contain a return statement (represented by the term return), followed by the value to be returned.

You do not need to add a return statement if you do not intend your Javascript function to return a value.

You may have observed that nothing occurs when you execute the code above. To make use of our function, we must first invoke or call it on the computer.

A function is denoted by the following notation:

The parenthesis (), when used together, represents an operator that begins a call to a function. In order to utilize the function, you must add any parameters you wish to use within the parentheses of the function called.

Let’s make use of the sum the function we just created above:

let sum = function(x, y){ return x + y; }
const chelseaPlayer = sum(1, 2);
const unitedPlayer = sum(2, 20);

console.log(chelseaPlayer, unitedPlayer);

Specifically, we make use of two separate functions in our example:

  • the sum() method, which takes two integers as parameters and returns the total of them
  • the console.log() method, which takes as inputs the newly generated variables we specified earlier

When we use sum(), we provide it with numbers, and as a byproduct, the value returned will be a number in both cases. This number is then entered into the console with the help of console.log().

Why Should You Make Use of Functions?

Perhaps the most compelling reason to utilize functions is that they enable us to reuse code and build modules to do operations that we wish to repeat on a regular basis. Even though the example above isn’t much more helpful than just using the + operator, you’ll find that functions are helpful when your program flow and code grows more elaborate and complex.


In the next lesson, we will talk about JavaScript Conditional Statements. You are missing some great topics that are available in the main course. Please enroll now?.

13 Comments

  1. May Allah bless and increase your knowledge.. thanks you Sir

  2. Thank you. I understand now

  3. Killed it again, Weldone Akin more grace to you 👍🏽

  4. God bless you, I’ll have to read more about (=== and !==) and (== and !=)

    I need to understand the principle attached to it

    Thank you.

  5. Alonge jonah says:

    Class one and two dusted make this comrade sleep small 😔

  6. Thank you for this, but I think I need a mentor on this stuff oh😂😂😂

  7. your teachings are straight to the point ,God bless you

  8. Ikomse Success says:

    Joined in today boss.

    Can I get a list of softwares I need

  9. Thank you so much for this boss, I can finally say I understand this now.

Leave a Reply

Your email address will not be published.