DEV Community

ItsYuvraj
ItsYuvraj

Posted on

26 Ways to log "Hello World" in Javascript.

Are you tired of the same old "Hello World" examples in JavaScript? Look no further! In this post, I'll show you 26 different ways to display "Hello World" using various methods, expressions, operators, and more. From the classic console.log to the Fetch API and even environment variables, there's something here for everyone. Follow along and see which approach you like best!

Let's get started!
• Common way

console.log("Hello World")
Enter fullscreen mode Exit fullscreen mode

• By variable

const hw = "Hello World";
console.log(hw)
Enter fullscreen mode Exit fullscreen mode

• By Template Literal

const tl = "Hello World";
console.log(`${tl}`)
Enter fullscreen mode Exit fullscreen mode

• By Ternary Operator

console.log(`${true ? "Hello World" : "Hello World"}`)
Enter fullscreen mode Exit fullscreen mode

• By Symbol

let s = Symbol("Hello World")
console.log(s) // Kinda Works? 
Enter fullscreen mode Exit fullscreen mode

• By a Regular Function

function hwf(string) {
  return string 
}
console.log(hwf("Hello World"))
Enter fullscreen mode Exit fullscreen mode

• By a Array

const arr = ["Hello World"]
console.log(arr, `${arr}`)
Enter fullscreen mode Exit fullscreen mode

• By Expression

if(true) {
  console.log("Hello World")
} else {
  console.log("Hello World")
}
Enter fullscreen mode Exit fullscreen mode

• By Object

const hwo = {
  word: "Hello World"
}
console.log(hwo["world"])
Enter fullscreen mode Exit fullscreen mode

• By Array Object, because why not

const hwoa = [
  {
    word: "Hello World"
  }
]
console.log(hwoa[0])
Enter fullscreen mode Exit fullscreen mode

• By .map() method

console.log(hwoa.map((v)=>{
  return `${v.word}`
})
)
Enter fullscreen mode Exit fullscreen mode

• by .forEach() method

hwoa.forEach(value => console.log(value))
Enter fullscreen mode Exit fullscreen mode

• by Arrow Function

const hwa = (str1) => {
  console.log(str1)
}

const hwa2 = (str2) => console.log(str2)

hwa("Hello World")
hwa2("Hello World")
Enter fullscreen mode Exit fullscreen mode

• by for loop

for (let i = 0; i < 1; i++) {
  console.log("Hello World")
}
Enter fullscreen mode Exit fullscreen mode

• by for of loop

for (let i of "Hello World") {
  console.log(i)
}
Enter fullscreen mode Exit fullscreen mode

• by for in loop

let a = "Hello World"
for (let i in a) {
  console.log(a)
}
Enter fullscreen mode Exit fullscreen mode

• by while loop

let i = 1
while (i < 2) {
  console.log("\n\nHello World")
   i++
}
Enter fullscreen mode Exit fullscreen mode

• by do while loop (18)

do {
 console.log("Hello World")
} while (false);
Enter fullscreen mode Exit fullscreen mode

• by class (19)

class HelloWorld {
  constructor() {
    console.log("Hello World");
  }
}

const myHelloWorld = new HelloWorld();
Enter fullscreen mode Exit fullscreen mode

• By environment variables (20)


// unfortunately include "or logical operator"
console.log(process.env["WORD"] || "Hello World") // i use "or logical operator" because if other user run my repl then user will not get hello world it get Error or undefined 
Enter fullscreen mode Exit fullscreen mode

• By .split() & .join() method (21)

const strArray = "H e l l o, W o r l d".split(" ");
const str = strArray.join("");

console.log(str); 
Enter fullscreen mode Exit fullscreen mode

• by .replace() method (22)

console.log("H_e_l_l_o W_o_r_l_d".replace(/_/g, ""));
Enter fullscreen mode Exit fullscreen mode

• by .push() method (23)

const hwp = []
hwp.push("Hello World")
console.log(hwp)
Enter fullscreen mode Exit fullscreen mode

• by .concat() method (24)

hwp.concat("Hello World")
console.log(hwp)
Enter fullscreen mode Exit fullscreen mode

• by Fetch API (25)

fetch('https://api.quotable.io/random')
  .then(response => response.json())
  .then(data => console.log('Hello World'))
  .catch(error => console.error(error)); // Sorry about that..
Enter fullscreen mode Exit fullscreen mode

• by alert (26)

alert("Hello World")
Enter fullscreen mode Exit fullscreen mode

Try on replit: Replit

Soon I will upload more ways!
Thanks for reading! Join my coding community

Top comments (0)