CodeBlanks Logo Icon CodeBlanks

Summary - String basics

JavaScript course · Beginner · Lesson 35/133

Build a greeting message combining strings, concatenation and template literals.

String basics recap

In this group you learned to create strings, use length and character access, concatenate with +, build template literals and use escape sequences.

A string is written with single or double quotes; "hello".length gives the character count and "hello"[0] the first character. The + operator joins two strings, and if one side is a number it converts it to a string.

Backticks and ${} give cleaner expression embedding. You write special characters with \n, \t and \\.

Exercise Build the greeting with +

Define firstName and lastName as strings ("Alex" and "Smith" for EN). Build greeting as firstName + " " + lastName. Log greeting with console.log.

Show example solution
const firstName = "Alex";
const lastName = "Smith";
const greeting = firstName + " " + lastName;
console.log(greeting);

Exercise Build a two line receipt

Declare product as "Pen" and price as the number 12.5. Build receipt as a template literal with ${product} on the first line, then a \n escape, then ${price} lira on the second line. Log receipt with console.log.

Show example solution
const product = "Pen";
const price = 12.5;
const receipt = `${product}\n${price} lira`;
console.log(receipt);

Test yourself

  1. Match each concept

    "str"
    Double quoted string
    str.length
    Character count
    a + b
    Concatenation
    `x ${y}`
    Template literal
  2. Which one allows expression interpolation?

    • 'hello ${name}'
    • `hello ${name}`
    • "hello ${name}"
    • [hello ${name}]
    Show answer

    `hello ${name}` — Correct. Only ${} inside backticks (a template literal) is evaluated.

All lessons in Beginner